Powershell Invoke-WebRequest 因 SSL/TLS 安全通道而失败



我正在尝试执行此powershell命令

Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

我收到此错误。"Invoke-WebRequest : 请求已中止: 无法创建 SSL/TLS 安全通道。" HTTPS请求似乎有效("https://google.com"),但有问题的请求无效。 如何使其工作或使用其他 powershell 命令来读取页面内容?

尝试使用这个

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

为了无耻地试图窃取一些选票,SecurityProtocol是一个具有[Flags]属性的Enum。 所以你可以这样做:

[Net.ServicePointManager]::SecurityProtocol = 
  [Net.SecurityProtocolType]::Tls12 -bor `
  [Net.SecurityProtocolType]::Tls11 -bor `
  [Net.SecurityProtocolType]::Tls

或者,由于这是PowerShell,你可以让它为你分析一个字符串:

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"

那么从技术上讲,您不需要知道TLS版本。

我在阅读此答案后从创建的脚本中复制并粘贴了此内容,因为我不想循环浏览所有可用的协议来找到有效的协议。 当然,如果你愿意,你可以这样做。

最后说明 - 我的PowerShell配置文件中有原始(减去SO编辑)语句,因此它出现在我现在开始的每个会话中。 这并非完全万无一失,因为仍然有一些网站失败,但我肯定会不那么频繁地看到有问题的消息。

错误的原因是Powershell默认使用TLS 1.0连接到网站,但网站安全性需要TLS 1.2。您可以通过运行以下任一命令来更改此行为以使用所有协议。您还可以指定单个协议。

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Ssl3
[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"
运行

这些命令后,请尝试运行命令:

Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

然后它会起作用。

如果像

我一样,以上方法都不起作用,那么可能也值得单独尝试较低的TLS版本。 我尝试了以下两种方法,但似乎没有解决我的问题:

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls

最后,只有当我以TLS 1.0为目标(特别是删除代码中的1.1和1.2)时,它才起作用:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls

本地服务器(正在尝试此操作)在TLS 1.2中很好,尽管远程服务器(之前被第三方"确认"为TLS 1.2正常)似乎并非如此。

希望这对某人有所帮助。

它对我有用...

if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
    {
    $certCallback = @"
        using System;
        using System.Net;
        using System.Net.Security;
        using System.Security.Cryptography.X509Certificates;
        public class ServerCertificateValidationCallback
        {
            public static void Ignore()
            {
                if(ServicePointManager.ServerCertificateValidationCallback ==null)
                {
                    ServicePointManager.ServerCertificateValidationCallback += 
                        delegate
                        (
                            Object obj, 
                            X509Certificate certificate, 
                            X509Chain chain, 
                            SslPolicyErrors errors
                        )
                        {
                            return true;
                        };
                }
            }
        }
    "@
        Add-Type $certCallback
     }
    [ServerCertificateValidationCallback]::Ignore()
Invoke-WebRequest -Uri https://apod.nasa.gov/apod/

确保先切换命令行管理程序:

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 
RUN Invoke-WebRequest -UseBasicParsing -Uri  'https://github.com/git-for-windows/git/releases/download/v2.25.1.windows.1/Git-2.25.1-64-bit.exe' -OutFile 'outfile.exe'

我还没有弄清楚原因,但重新安装.pfx证书(在当前用户和本地机器中)对我有用。

最新更新