我正在尝试执行这个powershell命令

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

得到这个错误。“调用- webrequest:请求被中止:无法创建SSL/TLS安全通道。”https请求似乎可以工作(“https://google.com”),但这个问题不存在。我怎么能让这个工作或使用其他powershell命令读取页面内容?


当前回答

此错误的原因是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/

那么它就会起作用。

其他回答

试试这个

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

我还不知道原因,但重新安装.pfx证书(在当前用户和本地机器)对我有用。

确保你先切换SHELL:

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'

这对我很有用……

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/

此错误的原因是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/

那么它就会起作用。