当我尝试使用时,Invoke-WebRequest
我得到一些奇怪的错误:
Invoke-WebRequest -Uri "https://idp.safenames.com/"
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
Run Code Online (Sandbox Code Playgroud)
我不确定是什么导致它,因为网站本身似乎很好.
即使堆栈溢出周围的所有"忽略ssl错误"函数,它仍然无法工作,让我想知道它是否与SSL有关.
Mat*_*sen 52
正如BaconBits所说,.NET版本> 4.5默认使用SSLv3和TLS 1.0.
您可以通过SecurityProtocol
使用ServicePointManager
类设置策略来更改此行为:
PS C:\> $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
PS C:\> [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
PS C:\> (Invoke-WebRequest -Uri "https://idp.safenames.com/").StatusCode
200
Run Code Online (Sandbox Code Playgroud)
这将适用于AppDomain中的所有请求(因此它仅适用于主机应用程序的当前实例).
GitHub和PSGallery中有一个模块可以立即管理这些设置:
Install-Module BetterTls -Scope CurrentUser
Import-Module BetterTls
Enable-Tls -Tls11 -Tls12
Run Code Online (Sandbox Code Playgroud)
基于此扫描,看起来URI不支持低于TLS 1.1的任何内容.
您使用的是哪个版本的Windows?如果您使用的是PowerShell v4.0或更低版本,则无法协商TLS 1.1或1.2连接,因为.Net Framework在.Net Framework 4.5之前不支持TLS 1.1或1.2.PowerShell v4.0是.Net 4.0.这意味着底层的System.Net.WebRequest类无法协商连接.我相信PowerShell v5.0是.Net 4.5或.Net 4.6,但我现在没有Win 10客户端来检查$PSVersionTable
.
您可以通过手动编写对WebRequest的调用并将协议指定为[System.Net.SecurityProtocolType]::Tls12
或来使其工作[System.Net.SecurityProtocolType]::Tls11
,但我不确定这是否可行.如果.Net 4.5是从我所看到的那样安装的,那应该可行.但是,我再也没有尝试过.
作为参考,我在Windows 7 x64/Powershell v4.0上获得与您完全相同的结果,并且我安装了.Net 4.5,但我从未尝试手动编写WebRequest编码.如果我从这里使用wget for Windows 1.11.4 (OpenSSL 0.9.8b,远在TLS 1.1和1.2之前),我也会收到错误,但是如果我从这里使用wget for Windows 1.17.1它可以正常工作(当前,更多或更少).
小智 7
这也可以永久改变
# set strong cryptography on 32 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
# set strong cryptography on 64 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Run Code Online (Sandbox Code Playgroud)