【问题标题】:RestSharp version > 107: How to implement NtlmAuthenticator?RestSharp版本> 107:如何实现NtlmAuthenticator?
【发布时间】:2022-06-13 20:27:31
【问题描述】:

我从一位同事那里得到了一个源代码,其中在 VB.NET 中使用 RestSharp(版本 106.15.0)从 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")

如何更改此代码以使其再次工作? 我读到 NtlmAuthenticator 现在是通过 ClientOptions 和 UseDefaultCredentials = true 定义的,但它不起作用。

到目前为止我的方法:

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) 行时,会抛出一个非特定的错误。

【问题讨论】:

    标签: vb.net restsharp ntlm-authentication


    【解决方案1】:

    我使用NtlmAuthenticator 在VB.NET 和C#.NET 中创建了一个非常简单的控制台应用程序,并且能够使代码与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);
                        }
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      相关资源
      最近更新 更多