Powershell 如何获取下载链接返回的文件名?

Powershell 如何获取下载链接返回的文件名?,powershell,Powershell,在浏览器中请求时,它返回要保存的文件名“VSCodeUserSetup-x64-1.27.2.exe” 我想获得相同的信息“VSCodeUserSetup-x64-1.27.2.exe”,因此我尝试: $webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable") $test = $webrequest.GetResponse() 但我得到了一个错误: 使用“0”参数调用“GetResponse”时出

在浏览器中请求时,它返回要保存的文件名“VSCodeUserSetup-x64-1.27.2.exe”

我想获得相同的信息“VSCodeUserSetup-x64-1.27.2.exe”,因此我尝试:

$webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable")
$test = $webrequest.GetResponse()
但我得到了一个错误:

使用“0”参数调用“GetResponse”时出现异常:“底层” 连接已关闭:发送时发生意外错误。“

这是正确的方法还是其他方法

我不明白为什么,因为我跟着
并且没有为GetResponse提供参数。

我可以通过执行以下操作重现该问题:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11
$webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable")
$test = $webrequest.GetResponse()
因此,这是一个TLS问题。您可以通过运行以下命令来检查TLS版本:

[Net.ServicePointManager]::安全协议

如果合适,请尝试按如下方式设置TLS版本:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable")
$test = $webrequest.GetResponse()
示例(TLS 1.1):

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11
Write-host "TLS Version: $([Net.ServicePointManager]::SecurityProtocol)"
$webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable")
$webRequest.GetResponse()

TLS Version: Tls11
Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."
At line:4 char:1
+ $webRequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-host "TLS Version: $([Net.ServicePointManager]::SecurityProtocol)"
$webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable")
$webRequest.GetResponse()

TLS Version: Tls12


IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {Content-MD5, X-Cache, x-ms-blob-type, x-ms-lease-status...}
SupportsHeaders         : True
ContentLength           : 45189424
ContentEncoding         : 
ContentType             : application/x-msdownload
CharacterSet            : 
Server                  : ECAcc (lha/8DA7)
LastModified            : 12/09/2018 17:38:17
StatusCode              : OK
StatusDescription       : OK
ProtocolVersion         : 1.1
ResponseUri             : https://az764295.vo.msecnd.net/stable/f46c4c469d6e6d8c46f268d1553c5dc4b475840f/VSCodeUserSetup-x64-1.27.2.exe
Method                  : GET
IsFromCache             : False
示例(TLS 1.2):

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11
Write-host "TLS Version: $([Net.ServicePointManager]::SecurityProtocol)"
$webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable")
$webRequest.GetResponse()

TLS Version: Tls11
Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."
At line:4 char:1
+ $webRequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-host "TLS Version: $([Net.ServicePointManager]::SecurityProtocol)"
$webRequest = [net.WebRequest]::Create("https://aka.ms/win32-x64-user-stable")
$webRequest.GetResponse()

TLS Version: Tls12


IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {Content-MD5, X-Cache, x-ms-blob-type, x-ms-lease-status...}
SupportsHeaders         : True
ContentLength           : 45189424
ContentEncoding         : 
ContentType             : application/x-msdownload
CharacterSet            : 
Server                  : ECAcc (lha/8DA7)
LastModified            : 12/09/2018 17:38:17
StatusCode              : OK
StatusDescription       : OK
ProtocolVersion         : 1.1
ResponseUri             : https://az764295.vo.msecnd.net/stable/f46c4c469d6e6d8c46f268d1553c5dc4b475840f/VSCodeUserSetup-x64-1.27.2.exe
Method                  : GET
IsFromCache             : False

我复制并粘贴了你的代码,对我来说很好。标题显示
x-ms-blob-type
,响应URI为.exe文件,文件大小正确。PowerShell 5.1和.net 4.7.03190