我正在尝试从在虚拟机上运行的本地主机项目发出网络请求。问题是我无法创建网络请求,我将收到以下错误:
“请求已中止:无法创建 SSL/TLS 安全通道”。
当我尝试使用 Postman(也来 self 的虚拟机)访问我在 webrequest 中调用的相同 API 时,它确实有效:
这行不通:
网络请求代码:
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://pokeapi.co/api/v2/pokemon/ditto/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject(responseBody);
}
catch (Exception ex)
{
throw ex;
}
响应:
这确实有效:
我已经尝试过的:
我已经搜索了这个错误以及所有添加以下代码行的解决方案:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
希望有人能帮忙,
提前致谢!
最佳答案
终于修好了!
因为我尝试调用的 API 是 https 而我自己的应用程序是 http,所以导致错误!
关于c# - 无法修复 "Could not create SSL/TLS secure channel"发出 web 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56257375/