RESTSHARP版本> 107:如何实现ntlmauthenticator?

发布于 2025-02-06 11:02:06 字数 1124 浏览 2 评论 0原文

我从同事那里获得了一个源代码,其中使用RestSharp(版本106.15.0)在VB.NET中从Web API检索数据。 我将RestSharp版本更新为108.0.1,并且代码不再可行。我发现RestSharp版本107有些事情发生了变化。但是我无法再使用代码了。

旧代码:

Dim restClient As New RestClient(server) With {
            .Timeout = 10000,
            .Authenticator = New NtlmAuthenticator(),
            .ThrowOnAnyError = True
        }
Dim response As IRestResponse
Dim restRequest = New RestRequest(sQ, Method.Post)
restRequest.AddHeader("content-type", "application/json")
restRequest.AddHeader(Settings.Default.AppIdKey, Settings.Default.AppIdValue)
restRequest.AddHeader("Accept-Language", "en")

如何更改此代码以使其再次工作? 我读到,现在通过clientoptions使用underefefaultCredentials = true来定义ntlmauthenticator,但它行不通。

到目前为止,我的方法是:

Dim uri As New Uri("url")
Dim restClientOptions As RestClientOptions = New RestClientOptions(uri)
restClientOptions.UseDefaultCredentials = True
restClientOptions.ThrowOnAnyError = True
Dim restClient = New RestClient(restClientOptions)

运行行DIM RESTCLIENT = new RestClient(RestClientOptions)时,会引发非特定的错误。

I got a source code from a colleague in which using RestSharp (version 106.15.0) in VB.NET retrieves data from a web api.
I updated the RestSharp version to 108.0.1 and the code no longer works. I found out that some things have changed with RestSharp version 107. But I can't get the code to work anymore.

Old Code:

Dim restClient As New RestClient(server) With {
            .Timeout = 10000,
            .Authenticator = New NtlmAuthenticator(),
            .ThrowOnAnyError = True
        }
Dim response As IRestResponse
Dim restRequest = New RestRequest(sQ, Method.Post)
restRequest.AddHeader("content-type", "application/json")
restRequest.AddHeader(Settings.Default.AppIdKey, Settings.Default.AppIdValue)
restRequest.AddHeader("Accept-Language", "en")

How do I change this code to make it work again?
I read that NtlmAuthenticator is now defined via ClientOptions with UseDefaultCredentials = true, but it doesn't work.

My approach so far:

Dim uri As New Uri("url")
Dim restClientOptions As RestClientOptions = New RestClientOptions(uri)
restClientOptions.UseDefaultCredentials = True
restClientOptions.ThrowOnAnyError = True
Dim restClient = New RestClient(restClientOptions)

When running the line Dim restClient = New RestClient(restClientOptions), a non-specific error is thrown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笑咖 2025-02-13 11:02:06

我在vb.netc#.net中使用ntlmauthenticator创建了一个非常简单的控制台应用程序 > V107。

vb.net:c#.net

Dim clientOptions As New RestClientOptions(BaseUrl) With
        {
            .UseDefaultCredentials = True
        }

        Using client As New RestClient(clientOptions)
            Dim request As New RestRequest(sQ, Method.Post)
            request.AddJsonBody(payload)
            Dim response As RestResponse = Await client.PostAsync(request)

            If response.IsSuccessful Then
                MsgBox(response.Content)
            End If
        End Using

RestClientOptions clientOptions = new RestClientOptions(BaseUrl)
                {
                    UseDefaultCredentials = true
                };

                using (RestClient client = new RestClient(clientOptions))
                {
                    string sQ = "...";
                    RestRequest request = new RestRequest(sQ, Method.Post);
                    request.AddJsonBody("JSON-Body");
                    RestResponse response = await client.PostAsync(request);
                    if (response.IsSuccessful)
                    {
                        Console.WriteLine(response.Content);
                    }
                }

I created a very simple console application in both VB.NET and C#.NET with NtlmAuthenticator and was able to get the code to work with Restsharp > v107.

VB.NET:

Dim clientOptions As New RestClientOptions(BaseUrl) With
        {
            .UseDefaultCredentials = True
        }

        Using client As New RestClient(clientOptions)
            Dim request As New RestRequest(sQ, Method.Post)
            request.AddJsonBody(payload)
            Dim response As RestResponse = Await client.PostAsync(request)

            If response.IsSuccessful Then
                MsgBox(response.Content)
            End If
        End Using

C#.NET

RestClientOptions clientOptions = new RestClientOptions(BaseUrl)
                {
                    UseDefaultCredentials = true
                };

                using (RestClient client = new RestClient(clientOptions))
                {
                    string sQ = "...";
                    RestRequest request = new RestRequest(sQ, Method.Post);
                    request.AddJsonBody("JSON-Body");
                    RestResponse response = await client.PostAsync(request);
                    if (response.IsSuccessful)
                    {
                        Console.WriteLine(response.Content);
                    }
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文