【发布时间】: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