Merge pull request #7676 from johnbeisner/master

Skip DefaultProxy resolution code if an exception is thrown
This commit is contained in:
John Beisner 2017-09-19 14:26:22 -07:00 committed by GitHub
commit 2956c548b9

View file

@ -172,14 +172,20 @@ function GetHTTPResponse([Uri] $Uri)
# HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet. # HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet.
Load-Assembly -Assembly System.Net.Http Load-Assembly -Assembly System.Net.Http
if(-not $ProxyAddress) if( -not $ProxyAddress) {
{ try {
# Despite no proxy being explicitly specified, we may still be behind a default proxy # Despite no proxy being explicitly specified, we may still be behind a default proxy
$DefaultProxy = [System.Net.WebRequest]::DefaultWebProxy; $DefaultProxy = [System.Net.WebRequest]::DefaultWebProxy;
if($DefaultProxy -and (-not $DefaultProxy.IsBypassed($Uri))) { if($DefaultProxy -and (-not $DefaultProxy.IsBypassed($Uri))) {
$ProxyAddress = $DefaultProxy.GetProxy($Uri).OriginalString $ProxyAddress = $DefaultProxy.GetProxy($Uri).OriginalString
$ProxyUseDefaultCredentials = $true $ProxyUseDefaultCredentials = $true
} }
} catch {
# Eat the exception and move forward as the above code is an attempt
# at resolving the DefaultProxy that may not have been a problem.
$ProxyAddress = $null
Say-Verbose("Exception ignored: $_.Exception.Message - moving forward...")
}
} }
if($ProxyAddress) { if($ProxyAddress) {
@ -188,17 +194,16 @@ function GetHTTPResponse([Uri] $Uri)
$HttpClient = New-Object System.Net.Http.HttpClient -ArgumentList $HttpClientHandler $HttpClient = New-Object System.Net.Http.HttpClient -ArgumentList $HttpClientHandler
} }
else { else {
$HttpClient = New-Object System.Net.Http.HttpClient $HttpClient = New-Object System.Net.Http.HttpClient
} }
# Default timeout for HttpClient is 100s. For a 50 MB download this assumes 500 KB/s average, any less will time out # Default timeout for HttpClient is 100s. For a 50 MB download this assumes 500 KB/s average, any less will time out
# 10 minutes allows it to work over much slower connections. # 10 minutes allows it to work over much slower connections.
$HttpClient.Timeout = New-TimeSpan -Minutes 10 $HttpClient.Timeout = New-TimeSpan -Minutes 10
$Response = $HttpClient.GetAsync($Uri).Result $Response = $HttpClient.GetAsync($Uri).Result
if (($Response -eq $null) -or (-not ($Response.IsSuccessStatusCode))) if (($Response -eq $null) -or (-not ($Response.IsSuccessStatusCode))) {
{
$ErrorMsg = "Failed to download $Uri." $ErrorMsg = "Failed to download $Uri."
if ($Response -ne $null) if ($Response -ne $null) {
{
$ErrorMsg += " $Response" $ErrorMsg += " $Response"
} }