3

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 ?

user94559
  • 59,196
  • 6
  • 103
  • 103
AVIK DUTTA
  • 736
  • 6
  • 23

2 Answers2

1

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.

Community
  • 1
  • 1
BendEg
  • 20,098
  • 17
  • 57
  • 131
1

Hi Avik I use this code for obtain my HttpWebResponse:

using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            //To obtain response body
                            using (Stream streamResponse = response.GetResponseStream())
                            {
                                using (StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8))
                                {
                                    var result = streamRead.ReadToEnd();

                                    if (response.Equals("1")) //It's 1 in my case when operation                           is complete!!
                                    {

                                    }
                                    else
                                    {

                                    }
                                }
                            }
                        }
                    }

I think you can use this, if you need more info or u can solve with my code advise me! Good luck AviK!