我需要消耗从VB.NET应用程序的Web资源。 我已经成功地检索访问令牌,并准备好使用它来使受保护资源的调用。 然而,每次我打电话受保护的资源我收到一个401未经授权响应,因为授权领域还没有被添加到头部。
这里是我的代码。
WebRequest = DirectCast(Net.WebRequest.Create(ApiUri), HttpWebRequest)
WebRequest.Method = "POST"
WebRequest.ContentType = "application/json"
WebRequest.ContentLength = Bytes.Length
Dim RequestStream As IO.Stream = WebRequest.GetRequestStream
RequestStream.Write(Bytes, 0, Bytes.Length)
RequestStream.Close()
WebRequest.Headers("Authorization") = "OAuth " & _
"oauth_version=""1.0""," & _
"oauth_nonce=""" & Nonce & """," & _
"oauth_timestamp=""" & TimeStamp & """," & _
"oauth_consumer_key=""" & ConsumerKey & """," & _
"oauth_token=""" & Token & """," & _
"oauth_signature_method=""HMAC-SHA1""," & _
"oauth_signature=""" & Signature & """"
WebResponse = DirectCast(WebRequest.GetResponse(), HttpWebResponse)
然后我监视使用招请求。 所有我在提琴手看到与如下所示(不包括体)401响应该请求。
请求
POST ***url*** HTTP/1.0
Content-Type: application/json
Host: ***host***
Content-Length: 45
Connection: Keep-Alive
响应
HTTP/1.0 401 Unauthorized
X-Powered-By: PHP/5.3.13
WWW-Authenticate: Basic realm="***realm***"
Content-type: application/json
Content-Length: 79
Connection: keep-alive
Date: Mon, 07 Jan 2013 01:13:22 GMT
Server: lighttpd/1.4.28
我到处都已经在互联网上阅读表明HttpWebRequest的应先挑战服务器并接收401响应,因为我在这里看到。 然后,它应该加入到头部授权领域再试一次,并得到200 OK应答。 这第二部分不会发生。 难道我不明白这是如何工作正常,还是我做错了什么?