加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用RESTSHARP时API返回错误

发布时间:2020-12-15 23:50:14 所属栏目:百科 来源:网络整理
导读:当使用RestSharp调用API时,我收到此错误: The underlying connection was closed: An unexpected error occurred on a send. 我已经验证我的客户端ID,密码,用户名和密码是否正确.我能够在没有PowerShell问题的情况下做到这一点. public string GetTokenForB
当使用RestSharp调用API时,我收到此错误:

The underlying connection was closed: An unexpected error occurred on
a send.

我已经验证我的客户端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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读