【问题标题】:PowerShell Error: The Underlying connection was closedPowerShell 错误:基础连接已关闭
【发布时间】:2019-03-01 06:23:51
【问题描述】:

我正在尝试使用 Invoke-WebRequest cmdlet(第一次)连接到 Sharp 打印机的 Web 界面。到目前为止,我的代码如下:

$cred = Get-Credential
$url = 'http://<IP address of printer>/login.html?/main.html'
$login = Invoke-WebRequest $url -SessionVariable printer -Method Get
$login.Forms[0].Fields.element10002 = $cred.UserName
$login.Forms[0].Fields.element10002 = 
$cred.GetNetworkCredential().Password
$mainPage = Invoke-WebRequest -Uri ($url + $login.Forms[0].Action) ` 
    -WebSession $printer -Body $login -Method Post

...但我不断收到此错误:

Invoke-WebRequest : The underlying connection was closed: The connection
was closed unexpectedly.
At line:6 char:13
+ $mainPage = Invoke-WebRequest -Uri ($url + $login.Forms[0].Action) -W 
...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: 
(System.Net.HttpWebRequest
:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : 
WebCmdletWebResponseException,Microsoft.Powe
rShell.Commands.InvokeWebRequestCommand

这行代码不会报错:

$login = Invoke-WebRequest $url -SessionVariable printer -Method Get

但是这条线可以:

$mainPage = Invoke-WebRequest -Uri ($url + $login.Forms[0].Action) ` 
    -WebSession $printer -Body $login -Method Post

我在 Windows 7 机器上使用 PS 版本 5.1 和 Tls12。经过一番谷歌搜索,这个问题似乎与 Tls 的版本有关。但我不确定要改成什么。

有人有什么想法吗? - @leah_cyberpadawan

【问题讨论】:

  • 默认情况下,Windows PowerShell 的 AppDomain 不支持 TLSv1.2,这可能是您的问题的原因。如果按照链接答案的建议不起作用,还可以考虑设置您的ServicePointManager[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
  • @TheIncorrigible1,感谢您的意见。我尝试了解决方案,但仍然出现错误。另一篇文章中的一个 cmets 说 PS 5.1 默认阻止 tls12 和 11。所以也许我需要以某种方式取消阻止它?
  • 它不会阻止它们,但它不会接受它们作为默认值。您可以通过查看[Net.ServicePointManager]::SecurityProtocol 来验证启用了哪些协议@您是否尝试过我的建议?
  • @TheIncorrigible1,当前启用的协议是 Ssl3、Tls、Tls11、Tls12,我确实尝试了您的代码。 Invoke 命令熟练错误。

标签: powershell webrequest


【解决方案1】:

在使用可能具有自签名证书或仅支持 TLS 1.2 的端点时,以下两种解决方案对我有用:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
Add-Type -TypeDefinition @'
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy
{
    public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest req, int certProblem)
    {
        return true;
    }
}
'@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

【讨论】:

    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    相关资源
    最近更新 更多