[英]API is returning error when using RESTSHARP
使用 RestSharp 調用 API 時出現此錯誤:
基礎連接已關閉:發送時發生意外錯誤。
我已驗證我的客戶端 ID、機密、用戶名和密碼正確無誤。 我可以在 PowerShell 中毫無問題地執行此操作。
public string GetTokenForBrightIdea()
{
RestClient restclient = new RestClient(_uri);
RestRequest request = new RestRequest() { Method = Method.POST };
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "password");
request.AddParameter("client_id", _clientId);
request.AddParameter("client_secret", _clientSecret);
request.AddParameter("username", _clientUsername);
request.AddParameter("password", _clientPassword);
var tResponse = restclient.Execute(request);
var responseJson = tResponse.Content;
return JsonConvert.DeserializeObject<Dictionary<string, object>>(
responseJson)["access_token"].ToString();
}
使用 RestSharp 完成這項工作時我錯過了什么?
事實證明,因為這個調用是HTTPS我需要添加以下代碼行
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
對於.Net 3.5和4.0,您可以嘗試在RestSharp客戶端初始化之前放置這行代碼:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
這對我很好:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;
我想知道你在哪里添加這一行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.