0

说到Web开发,我知道的很少...

我从以下站点找到了一些代码和解释。 https://dev.twitter.com/docs/auth/implementing-sign-twitter

最终,我想用 twitter 实现登录。但是我无法将这些 POST Web 请求重写为 ac# HttpWebRequest 格式,我可以在我们的其他应用程序中重复使用这种格式。如果我们检查第一个 web 请求...

POST /oauth/request_token HTTP/1.1
 User-Agent: themattharris' HTTP Client
 Host: api.twitter.com
 Accept: */*
 Authorization: 
    OAuth oauth_callback="http%3A%2F%2Flocalhost%2Fsign-in-with-twitter%2F",
          oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w",
          oauth_nonce="ea9ec8429b68d6b77cd5600adbbb0456",
          oauth_signature="F1Li3tvehgcraF8DMJ7OyxO4w9Y%3D",
          oauth_signature_method="HMAC-SHA1",
          oauth_timestamp="1318467427",
          oauth_version="1.0"

我想将其转换为有效的 HttpWebRequest。迄今。我的代码看起来像这样......

HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth/request_token");

        ASCIIEncoding encoding = new ASCIIEncoding();

        httpReq.Method = "POST";
        httpReq.ContentType = "application/x-www-form-urlencoded";
        httpReq.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

不幸的是,我确实做到了……我不知道这些请求是如何工作的。我需要包括其余数据并拨打电话。但我被困住了。任何帮助将不胜感激。

4

1 回答 1

1

尝试这个 :

ASCIIEncoding encoder = new ASCIIEncoding();
byte[] data = encoder.GetBytes(serializedObject); // the data you wanted to send

HttpWebRequest request = new WebRequest.Create("https://api.twitter.com/oauth/request_token") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

request.GetRequestCode().Write(data, 0, data.Length);

还有一个可能的重复(类似问题):为什么我得到 411 Length required 错误?

于 2013-08-22T12:26:23.090 回答