Search code examples
c#windows-phonewindows-phone-8.1dropbox

how to store the return value of https POSTmethod in C#


let just say i want to save the value of the

 POST` method "[https://api.dropbox.com/1/oauth/request_token][1]" 

in a variable called

 `accessToken` of type `string`

(lets just asume that the post method retuns string for the sake of simplicity) in c# .. How to do it ?


Solution

  • First you have to decalre a string variable, like this:

    string httpReturnValue = "";
    

    To get the value and store it in the string you have to do this:

    var request = (HttpWebRequest)WebRequest.Create("YOUR URL");
    
    // For example
    var postData = "thing1=hello";
        postData += "&thing2=world";
    
    var data = Encoding.ASCII.GetBytes(postData);
    
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;
    
    using (var stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }
    
    var response = (HttpWebResponse)request.GetResponse();
    
    var httpReturnValue= new StreamReader(response.GetResponseStream()).ReadToEnd();
    

    The code is from here: HTTP request with post.