在 VB.NET 中访问 REST WebService
我正在尝试访问 REST Web 服务,但是我的代码无法正常工作并不断抛出 HTTP 401 错误,
请参阅 Wireshark 中初始 ssl 请求的示例屏幕截图:
这是对具有相同详细信息的同一网址的 Curl 调用。问题是在我的 VB.NET 版本下,SSL 数据包的“扩展名:server_name”(及相关)部分丢失,我相信这导致服务器无法在接下来的几个数据包中回复服务器密钥交换。
下面是 VB.NET 代码,
Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(UrlString), HttpWebRequest)
webRequest.Method = "GET"
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.Credentials = New NetworkCredential(username, password)
Dim response As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
这在最后一行失败(HTTP 401 错误),问题似乎与我的代理无关,并且服务器的用户凭据是正确的,因为此请求是通过 Curl 和 wget 进行的。
我已经尝试过设置 AuthenticationLevel 以及手动设置 Authorization 标头,并且 PreAuthenticate 似乎也没有改变这个问题。
I am trying to access a REST webservice, however my code doesn't work and keeps throwing a HTTP 401 error
see this example screen cap from Wireshark of the initial ssl request:
This is from a Curl call to the same web address with the same details. The issue is that under my VB.NET version the "Extension: server_name" (and related) part of the SSL packet is missing, I believe that this is causing the server to not reply with the Server key exchange in the next couple packets.
Below is the VB.NET code
Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(UrlString), HttpWebRequest)
webRequest.Method = "GET"
webRequest.ContentType = "application/x-www-form-urlencoded"
webRequest.Credentials = New NetworkCredential(username, password)
Dim response As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
This fails on the last line (HTTP 401 error), issue does not seem to be related to my proxy and the user credentials are correct for the server as this request works from Curl and wget.
I have already tried setting the AuthenticationLevel as well as manually setting the Authorization header, and PreAuthenticate doesn't seem to change this issue either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是 WebService 没有回复有效的 HTTP401 WWW-Authenticate 标头,而只是返回如下代码:
因此我必须手动添加身份验证标头,如下所示:
http://devproj20.blogspot.com/2008/02/分配-basic-authorization-http.html
代码:
The answer was that the WebService was not replying with a valid HTTP401 WWW-Authenticate header, instead was just returning with code like:
So I had to manually add the authentication header as shown here:
http://devproj20.blogspot.com/2008/02/assigning-basic-authorization-http.html
code: