我正在尝试使用RestSharpe.NetCore(v.105.2.3.0)发出GET请求,例如C#中的https://pokeapi.co/api/v2/pokemon/ditto
。
但是,下面的代码出现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);
}
任何想法为何以及如何解决此问题?
在进行连接以使用TLS1.1或1.2连接到站点之前,添加此行。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
对于代理,将代理添加到客户端
client.Proxy = new WebProxy("127.0.0.1", 8888);
详细信息请参见this link