Pergunta

When it comes to web development, I know very very little...

I have found some code and explanations from the following site. https://dev.twitter.com/docs/auth/implementing-sign-twitter

Ultimately, I want to implement login with twitter. But I am having trouble rewriting those POST web requests into a c# HttpWebRequest format that I can reuse in the rest of our apps. If we examine the first webrequest made...

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"

I want to transform that into a working HttpWebRequest. Thus far. My code looks like this...

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";

This is unfortunately how far I did get... I don't know how these requests work. I need to include the rest of the data and make the call. But I am stuck. Any help would be greatly appreciated.

Foi útil?

Solução

Try this :

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

Also a possible dublicate (similar question) : Why I get 411 Length required error?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top