使用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进行此操作时我缺少什么?
答案 0 :(得分:13)
事实证明,因为这个调用是HTTPS我需要添加以下代码行
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
答案 1 :(得分:3)
对于.Net 3.5和4.0,您可以尝试在RestSharp客户端初始化之前放置以下代码:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
答案 2 :(得分:1)
这对我来说很好:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;