来自.net的错误调用API“无法创建SSL/TLS安全频道”。

发布于 2025-02-11 03:04:16 字数 1619 浏览 1 评论 0原文

当调用某些API时,我会从下面的代码中获取“无法创建SSL/TLS安全频道”(可用于某些API),但仅在从Windows 2012 Server运行时。它在我的笔记本电脑上正常工作。我发现的另一件事是,我可以从服务器上称为邮递员的失败API罚款,但是如果我使用PowerShell,则会失败。

它似乎与.NET有关,但我尝试更新为.NET 4.8而没有任何成功。

对还要尝试什么想法?

Console.WriteLine("Make Reuest Starting");
            if (!string.IsNullOrWhiteSpace(generalConfig?.ProxyAddress))
            {
                WebProxy webProxy = new WebProxy(generalConfig.ProxyAddress);
                webProxy.Credentials = new NetworkCredential(generalConfig.ProxyUsername, generalConfig.ProxyPassword,
                    generalConfig.ProxyDomain);
                WebRequest.DefaultWebProxy = webProxy;
            }
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            WebRequest request = WebRequest.Create(apiUrl) as HttpWebRequest;
            request.Timeout = 40000;

            if (!(string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)))
            {
               
                request.Credentials = new NetworkCredential(username, password);
            }
            Console.WriteLine("Pre web request log");
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                Console.WriteLine(response.StatusCode);
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    responseData = reader.ReadToEnd();
                    //Console.WriteLine(responseData);
                }
            }

I'm getting the error "Could not create SSL/TLS secure channel" from the code below when calling certain APIs (it works for some APIs) but only when run from a Windows 2012 server. It works fine on my laptop. The other thing I have found is I can call the failing APIs fine from Postman from the server but if I use Powershell it fails.

It seems to be something related to .Net but I have tried updating to .Net 4.8 without any success.

Any thoughts on what else to try?

Console.WriteLine("Make Reuest Starting");
            if (!string.IsNullOrWhiteSpace(generalConfig?.ProxyAddress))
            {
                WebProxy webProxy = new WebProxy(generalConfig.ProxyAddress);
                webProxy.Credentials = new NetworkCredential(generalConfig.ProxyUsername, generalConfig.ProxyPassword,
                    generalConfig.ProxyDomain);
                WebRequest.DefaultWebProxy = webProxy;
            }
            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            WebRequest request = WebRequest.Create(apiUrl) as HttpWebRequest;
            request.Timeout = 40000;

            if (!(string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)))
            {
               
                request.Credentials = new NetworkCredential(username, password);
            }
            Console.WriteLine("Pre web request log");
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                Console.WriteLine(response.StatusCode);
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    responseData = reader.ReadToEnd();
                    //Console.WriteLine(responseData);
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文