在 VB.NET 中访问 REST WebService

发布于 2024-09-14 07:00:51 字数 891 浏览 2 评论 0原文

我正在尝试访问 REST Web 服务,但是我的代码无法正常工作并不断抛出 HTTP 401 错误,

请参阅 Wireshark 中初始 ssl 请求的示例屏幕截图:

WireShark Screenshot

这是对具有相同详细信息的同一网址的 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:

WireShark Screenshot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2024-09-21 07:00:51

答案是 WebService 没有回复有效的 HTTP401 WWW-Authenticate 标头,而只是返回如下代码:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Error>
    <ErrorCode>NO_SUITABLE_AUTHENTICATION_METHOD</ErrorCode>
    <ErrorDescription>No suitable active authentication mechanism found to authorise request - authorise using supported mechanisms</ErrorDescription>
    <RequestID>gcqsrtvn</RequestID>
    <SystemTime>2010-08-12T09:14:21.831+10:00</SystemTime>
</Error>

因此我必须手动添加身份验证标头,如下所示:
http://devproj20.blogspot.com/2008/02/分配-basic-authorization-http.html

代码:

Dim authBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(String.Format("{0}:{1}", username, password).ToCharArray())
webRequest.Headers("Authorization") = "Basic " + Convert.ToBase64String(authBytes)

The answer was that the WebService was not replying with a valid HTTP401 WWW-Authenticate header, instead was just returning with code like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Error>
    <ErrorCode>NO_SUITABLE_AUTHENTICATION_METHOD</ErrorCode>
    <ErrorDescription>No suitable active authentication mechanism found to authorise request - authorise using supported mechanisms</ErrorDescription>
    <RequestID>gcqsrtvn</RequestID>
    <SystemTime>2010-08-12T09:14:21.831+10:00</SystemTime>
</Error>

So I had to manually add the authentication header as shown here:
http://devproj20.blogspot.com/2008/02/assigning-basic-authorization-http.html

code:

Dim authBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(String.Format("{0}:{1}", username, password).ToCharArray())
webRequest.Headers("Authorization") = "Basic " + Convert.ToBase64String(authBytes)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文