PowerShell Invoke-WebRequest 抛出 WebCmdletResponseException [英] PowerShell Invoke-WebRequest throws WebCmdletResponseException

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

问题描述

当执行 Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.html 行时,PowerShell 抛出 WebCmdletResponseException.我怎样才能获得有关它的更多信息,以及可能导致这种情况的原因是什么?虽然我可以使用 Python 成功获取页面内容,但在 PowerShell 中它会引发异常.

完全例外:

<块引用>

Invoke-WebRequest :基础连接已关闭:发送时发生意外错误.在行:1 字符:1+ Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.html+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc选项+ FullQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

解决方案

这是因为 Invoke-WebRequest 在底层使用了 HttpWebRequest,除了最近的.Net 版本默认使用 SSLv3 和 TLSv1.

您可以通过查看当前值来看到这一点:

[System.Net.ServicePointManager]::SecurityProtocol

您要连接的站点仅支持 TLS 1.2.

您可以更改允许的协议,但它会在您的应用程序运行期间全局应用:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

这会覆盖该值.

当然,这会破坏您的应用程序中依赖与不支持 TLS 1.2 的服务器的连接的任何其他内容

一种安全的方法可能是添加 TLS 1.2:

[System.Net.ServicePointManager]::SecurityProtocol] = ([System.Net.ServicePointManager]::SecurityProtocol -bor[System.Net.SecurityProtocolType]::Tls12)#括号是为了可读性

在偶然情况下,这仍然会导致其他站点出现问题(不确定是什么,也许某个站点说它接受 TLS 1.2 但它的实现被破坏而其 TLS 1.0 工作正常?),您可以保存以前的值并恢复它.

$cur = [System.Net.ServicePointManager]::SecurityProtocol]尝试 {[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.html} 最后 {[System.Net.ServicePointManager]::SecurityProtocol = $cur}

When executing the line Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.html PowerShell throws WebCmdletResponseException. How can I get more information about it, and what may be causing this? While I can successfully get the contents of the page using Python, but in PowerShell it throws an exception.

Full exception:

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.html
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

解决方案

This is because Invoke-WebRequest uses HttpWebRequest under the hood, which in all but the most recent versions of .Net defaults to using SSLv3 and TLSv1.

You can see this by looking at the current value:

[System.Net.ServicePointManager]::SecurityProtocol

The site you're connecting to only supports TLS 1.2.

You can change the allowed protocols, but it applies globally during your application's run:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

This overwrites the value.

Of course that would break anything else in your application which relies on a connection to a server that doesn't support TLS 1.2

A safe method might be to add TLS 1.2:

[System.Net.ServicePointManager]::SecurityProtocol] = (
    [System.Net.ServicePointManager]::SecurityProtocol -bor 
    [System.Net.SecurityProtocolType]::Tls12
)

# parentheses are for readability

On the off-chance this still causes a problem for other sites (not sure what, maybe a site that says it accepts TLS 1.2 but its implementation is broken while its TLS 1.0 works fine?), you can save the previous value and restore it.

$cur = [System.Net.ServicePointManager]::SecurityProtocol]
try {
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest -Uri https://www.freehaven.net/anonbib/date.html
} finally {
    [System.Net.ServicePointManager]::SecurityProtocol = $cur
}

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

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