Как получить имя файла, возвращаемое по ссылке для скачивания? - PullRequest
0 голосов
/ 15 сентября 2018

При запросе https://aka.ms/win32-x64-user-stable в браузере он возвращает это имя файла "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()

Но я получаю сообщение об ошибке:

Исключение вызывает" GetResponse "с аргументом" 0 ":«Базовое соединение было закрыто: при отправке произошла непредвиденная ошибка.»

Это правильный метод или что-то еще?

Я не понимаю, почему, потому что я следовал https://learn -powershell.net / 2011/02/11 / using-powershell-to-query-web-site-information / и для GetResponse нет аргументов.

1 Ответ

0 голосов
/ 15 сентября 2018

Я могу воспроизвести проблему, выполнив следующее:

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

Так что понятно, что это проблема TLS.Вы можете проверить версию TLS, выполнив:

[Net.ServicePointManager]::SecurityProtocol

и, если необходимо, попробуйте установить версию 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

Пример (TLS 1.2):

[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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...