首页 > 解决方案 > 无法使用 RestSharp.NetCore 发出 GET 请求

问题描述

我正在尝试使用 RestSharpe.NetCore(v.105.2.3.0) 发出 GET 请求,例如https://pokeapi.co/api/v2/pokemon/ditto在 C# 中。

但是,Error: SecureChannelFailure (Unable to read data from the transport connection: Connection reset by peer.)下面的代码出现错误。

IRestClient _client;

protected override async void OnCreate(Bundle bundle)
{
    ...
    var baseurl = "https://pokeapi.co/api/v2/pokemon/";
    var apiMethod = "ditto";
    var request = new RestRequest(apiMethod, Method.GET);

    this._client = new RestClient();
    this._client.BaseUrl = new Uri(baseurl);
    TaskCompletionSource<IRestResponse> taskCompletion = new TaskCompletionSource<IRestResponse>();
    var json = this._client.ExecuteAsync<object>(request, r => taskCompletion.SetResult(r));
    var response = (object)(await taskCompletion.Task);
}

任何想法为什么以及如何解决这个问题?

标签: c#restrestsharp

解决方案


在建立连接之前添加此行以使用 TLS1.1 或 1.2 连接到站点。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;

对于代理,将您的代理添加到客户端

client.Proxy = new WebProxy("127.0.0.1", 8888);

有关详细信息,请参阅此链接


推荐阅读