如何在C#中存储https POST方法的返回值

时间:2014-11-06 08:54:28

标签: c# windows-phone windows-phone-8.1 dropbox

我只想说要保存

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

在一个名为

的变量中
 `accessToken` of type `string`

(lets just asume that the post method retuns string for the sake of simplicity)  在c#.. 怎么做?

2 个答案:

答案 0 :(得分:1)

首先,您必须声明一个字符串变量,如下所示:

string httpReturnValue = "";

要获取值并将其存储在字符串中,您必须执行此操作:

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();

代码来自此处:HTTP request with post

答案 1 :(得分:1)

Hi Avik我使用此代码获取我的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
                                    {

                                    }
                                }
                            }
                        }
                    }

我认为您可以使用此功能,如果您需要更多信息,或者您可以使用我的代码解决建议!祝你好运AviK!