使用RESTSHARP时API返回错误 [英] API is returning error when using RESTSHARP
本文介绍了使用RESTSHARP时API返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用RestSharp调用API时,出现此错误:
When use RestSharp to call an API I get this error:
基础连接已关闭:发生意外错误 发送.
The underlying connection was closed: An unexpected error occurred on a send.
我已验证我的客户ID,机密,用户名和密码正确无误.我能够做到这一点而在PowerShell中没有问题.
I've verified that my client ID, secret, username, and password are correct. I'm able to do this without issues in 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进行这项工作时,我缺少什么?
What am I missing when using RestSharp to make this work?
推荐答案
因此事实证明,由于此调用是HTTPS,因此我需要添加以下代码行
So it turns out that because this call was HTTPS i needed to add the following line of code
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
这篇关于使用RESTSHARP时API返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文