c# - 无法修复“无法创建 SSL/TLS 安全通道”发出 webrequest
问题描述
我正在尝试从在虚拟机上运行的本地主机项目发出网络请求。问题是我无法创建 webrequest,我将收到以下错误:
“请求被中止:无法创建 SSL/TLS 安全通道”。
当我尝试使用 Postman(也来自我的虚拟机)访问我的 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,所以导致了错误!
推荐阅读
- react-native - React-Navigation:嵌套导航器中的导航
- servicestack - Service Stack 自定义 AutoQuery
- rnn - 修改 RNN CuDNN 示例代码以使用 CUDNN_DATA_INT8
- rest - SOAPUI:需要示例 REST API 以使用 SOAPUI 工具进行测试
- itext - iText:将 pdf 字节插入打开的文档
- npm - 将入口点更改为 npm 依赖项
- sql-server - 从 Visual Basic api 读取 SQL Server 存储过程
- angular - Angular 6 SASS 嵌套在模块级别不起作用
- android - 房间迁移失败回退android
- r - 无法为 GPU 编译/安装 LightGBM