我從一位同事那里得到了一個源代碼,其中在 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 現在是通過ClientOptionswith定義的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)
運行 lineDim restClient = New RestClient(restClientOptions)時,會引發非特定錯誤。
uj5u.com熱心網友回復:
我在兩者中都創建了一個非常簡單的控制臺應用程式,并且VB.NET能夠使代碼與 Restsharp > v107 一起使用。C#.NETNtlmAuthenticator
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490853.html
