首页 > 解决方案 > 无法修复“无法创建 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;

希望有人能帮忙,

提前致谢!

标签: c#apisecuritysslhttpwebrequest

解决方案


终于修好了!

因为我试图调用的API是https,而我自己的应用程序是http,所以导致了错误!


推荐阅读