c# - 无法使用 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);
}
任何想法为什么以及如何解决这个问题?
解决方案
在建立连接之前添加此行以使用 TLS1.1 或 1.2 连接到站点。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
对于代理,将您的代理添加到客户端
client.Proxy = new WebProxy("127.0.0.1", 8888);
有关详细信息,请参阅此链接
推荐阅读
- excel - 当单元格打开并选择另一个工作表时,Workbook_sheetChange 崩溃
- python-3.x - cx_oracle 在给定的 conda 环境中不起作用,但在另一个环境中起作用
- reactjs - 在 react-query 中处理未经授权的请求
- r - 有没有办法只将几个数字字符替换为 R 中的日期?
- mysql - 是否可以将本地 django 项目连接到在 AWS EC2 上运行的 MySql?如何?
- javascript - Javascript 函数只工作一次(PHP 文件)
- reactjs - 运行单元测试时,useEffect 挂钩中的 React useState 挂钩未设置状态
- python - 我是否通过使用 sklearn 的余弦相似度方法和 K-means 算法正确地对用户进行聚类?
- database - KDB/Q:如何在给定时间戳之前获取带有时间戳的记录
- javascript - 带有非堆叠条形、堆叠条形和线条的 Google Charts ComboChart