Invoke-WebRequest SSL 失败? [英] Invoke-WebRequest SSL fails?

查看:95
本文介绍了Invoke-WebRequest SSL 失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 Invoke-WebRequest 时,出现了一些奇怪的错误:

When I try to use Invoke-WebRequest I'm getting some weird error:

Invoke-WebRequest -Uri "https://idp.safenames.com/"

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.

我不确定是什么原因造成的,因为网站本身看起来不错.

I'm not sure what's causing it, as the website itself seems fine.

即使有围绕 stackoverflow 的所有忽略 ssl 错误"功能,它仍然不起作用,让我怀疑它是否与 SSL 有关.

Even with all the "ignore ssl errors" functions around stackoverflow, it's still not working, making me wonder if it's related to SSL at all.

推荐答案

As BaconBits 笔记,.NET 版本 > 4.5默认使用 SSLv3 和 TLS 1.0.

As BaconBits notes, .NET version > 4.5 uses SSLv3 and TLS 1.0 by default.

您可以通过设置 SecurityProtocol 策略与 ServicePointManager 类:

You can change this behavior by setting the SecurityProtocol policy with the ServicePointManager class:

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

这将适用于 AppDomain 中的所有请求(因此它仅适用于宿主应用程序的当前实例).

This will apply to all requests in the AppDomain (so it only applies to the current instance of the host application).

现在有 一个模块在 GitHub 和 PSGallery 中可以管理这些设置:

There's a module on GitHub and in PSGallery that can manage these settings now:

Install-Module BetterTls -Scope CurrentUser
Import-Module BetterTls
Enable-Tls -Tls11 -Tls12

这篇关于Invoke-WebRequest SSL 失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆