vb.net .NET HttpWebRequest oAuth 401 未经授权
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14188938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET HttpWebRequest oAuth 401 Unauthorized
提问by user1227445
I need to consume a web resource from a VB.NET app. I have successfully retrieved the access token and am ready to use it to make calls to the protected resource. However, everytime I call the protected resource I receive a 401 Unauthorized response because the Authorization field has not been added to the header.
我需要使用来自 VB.NET 应用程序的 Web 资源。我已成功检索访问令牌并准备使用它来调用受保护的资源。但是,每次我调用受保护的资源时,我都会收到 401 Unauthorized 响应,因为 Authorization 字段尚未添加到标头中。
Here is my code.
这是我的代码。
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)
I then monitor the request using Fiddler. All I see in Fiddler is the request with the 401 response as shown below (excluding the bodies).
然后我使用 Fiddler 监控请求。我在 Fiddler 中看到的只是带有 401 响应的请求,如下所示(不包括正文)。
Request
要求
POST ***url*** HTTP/1.0
Content-Type: application/json
Host: ***host***
Content-Length: 45
Connection: Keep-Alive
Response
回复
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
Everywhere I've read on the internet indicates that HttpWebRequest should first challenge the server and receive a 401 response as I'm seeing here. It should then try again with the Authorization field added to the header and get the 200 OK response. This second part doesn't happen. Am I not understanding how this works correctly, or am I doing something wrong?
我在互联网上读到的任何地方都表明 HttpWebRequest 应该首先挑战服务器并接收 401 响应,正如我在这里看到的那样。然后它应该再次尝试将 Authorization 字段添加到标头并获得 200 OK 响应。这第二部分不会发生。我是不是不明白这是如何正确工作的,还是我做错了什么?
采纳答案by user1227445
Turns out you need to add the content using a BinaryWriter, not a Stream.
事实证明,您需要使用 BinaryWriter 而不是 Stream 添加内容。
So instead of this.
所以,而不是这个。
WebRequest.ContentLength = Bytes.Length
Dim RequestStream As IO.Stream = WebRequest.GetRequestStream
RequestStream.Write(Bytes, 0, Bytes.Length)
RequestStream.Close()
Do this.
做这个。
Using Writer As New IO.BinaryWriter(WebRequest.GetRequestStream)
Writer.Write(Bytes)
End Using

