2014-11-06 104 views
3

让只说我想保存在一个名为如何HTTPS的PostMethod的返回值存储在C#

`accessToken` of type `string` 

(lets just asume that the post method retuns string for the sake of simplicity)在C# 变量的

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

值.. 怎么做 ?

+0

有很多方法可以做到这一点。你是如何执行它的?你的代码有什么问题? – Reniuz 2014-11-06 08:59:56

+0

我是新来的c#编程,你能帮我一些链接教程.. PLZ – 2014-11-06 09:02:55

+0

在你的标签,你的意思是字符串,而不是春天? – BendEg 2014-11-06 09:08:26

回答

1

首先,你必须decalre一个字符串变量,就像这样:

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

+0

谢谢你..长住[BendEg!](http://stackoverflow.com/users/2630261/bendeg) – 2014-11-06 13:39:48

1

嗨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 
            { 

            } 
           } 
          } 
         } 
        } 

我想你可以使用这个,如果你需要更多的信息或U可以用我的代码解决了告诉我!祝你好运AviK!

+0

谢谢你们! – 2014-11-06 13:41:00

+0

但是这段代码适合你吗?别客气! :) – 2014-11-07 07:55:59