gpt4 book ai didi

.net - “调用-WebRequest : The underlying connection was closed: An unexpected error

转载 作者:行者123 更新时间:2023-12-02 06:18:04 26 4
gpt4 key购买 nike

下面是我的自定义脚本扩展中的代码

$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile $env:C:\temp\azcopy.zip

当我运行自定义脚本扩展时,出现以下错误

"Invoke-WebRequest : The underlying connection was closed: An unexpected error

有什么解决办法吗?

最佳答案

该错误非常具体,这使得这可能是您的主机或企业环境中的环境问题,因为您发布的代码应该/确实可以按原样工作。

# Tested on a few lab hosts
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile $env:C:\temp\azcopy.zip -Verbose
Get-ChildItem C:\temp

# Results
<#
VERBOSE: GET https://aka.ms/downloadazcopy-v10-windows with 0-byte payload
VERBOSE: received 9317519-byte response of content-type application/zip


Directory: C:\temp


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/11/2020 8:45 PM 9317519 azcopy.zip
#>

然而,您实际上只需要在代码顶部添加它。

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

您可以通过运行代码来了解有关错误的更多信息,让错误发生,然后使用

$Error[0] | Format-List -Force

您还可以使用 Trace-Command 了解更多信息cmdlet。

Trace-Command -Name metadata,parameterbinding,cmdlet -Expression {
Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile $env:C:\temp\azcopy.zip
} -PSHost
# Results
<#
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Invoke-WebRequest]
DEBUG: ParameterBinding Information: 0 : BIND arg [https://aka.ms/downloadazcopy-v10-windows] to parameter [Uri]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Uri]
DEBUG: ParameterBinding Information: 0 : Trying to convert argument value from System.String to System.Uri
DEBUG: ParameterBinding Information: 0 : CONVERT arg type to param type using LanguagePrimitives.ConvertTo
DEBUG: ParameterBinding Information: 0 : CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [https://aka.ms/downloadazcopy-v10-windows]
DEBUG: ParameterBinding Information: 0 : Executing VALIDATION metadata: [System.Management.Automation.ValidateNotNullOrEmptyAttribute]
DEBUG: ParameterBinding Information: 0 : BIND arg [https://aka.ms/downloadazcopy-v10-windows] to param [Uri] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [\temp\azcopy.zip] to parameter [OutFile]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [\temp\azcopy.zip] to param [OutFile] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Invoke-WebRequest]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Invoke-WebRequest]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
#>

当尝试访问远程资源时,最好在采取行动之前测试事件/连接。通常这是 Test-Connection/Test-NetConection 的事情,但在您的情况下,Invoke-* cmdlet 之一会更谨慎。

Invoke-WebRequest -Uri 'https://aka.ms/downloadazcopy-v10-windows' -UseBasicParsing

# Results
<#
StatusCode : 200
StatusDescription : OK
Content : {80, 75, 3, 4...}
RawContent : HTTP/1.1 200 OK
Content-MD5: HjZjoPa87mg1bK4eVQqASQ==
x-ms-request-id: aa4341fb-e01e-00a6-0c94-810077000000
x-ms-version: 2009-09-19
x-ms-lease-status: unlocked
x-ms-blob-type: BlockBlob
Connect...
Headers : {[Content-MD5, HjZjoPa87mg1bK4eVQqASQ==], [x-ms-request-id, aa4341fb-e01e-00a6-0c94-810077000000], [x-ms-version, 2009-09-19],
[x-ms-lease-status, unlocked]...}
RawContentLength : 9317519
#>

另请参阅 article .

关于.net - “调用-WebRequest : The underlying connection was closed: An unexpected error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63855131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com