Repository navigation
Description
Steps to reproduce
- Install Windows Server Nano (10.0.14393)
- Connect to the server using PS Remoting, then use PowerShell Core (the built-in version) to download a file, e.g. try to download .NET Core 1.1 ZIP package:
Invoke-Webrequest -Uri 'https://go.microsoft.com/fwlink/?LinkID=835028' -OutFile C:\netcore11.zip -MaximumRedirection 10
Expected behavior
File is downloaded, HTTP 302 Redirect returned by go.microsoft.com is handled properly.
Actual behavior
The following error occurs:
invoke-webrequest : Response status code does not indicate success: 302 (Moved Temporarily).
And there is no workaround, because the thrown exception is a HttpRequestException thrown by EnsureSuccessStatusCode(), which loses all header information (so, unable even to access the Location header).
Issue is still present in the master branch:
https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebRequestPSCmdlet.CoreClr.cs#L352
EnsureSuccessStatusCode() should not be used, as it doesn't support redirects, and it also obfuscates the exception, making it impossible to access the returned headers/body even if the exception is caught.
Environment data
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
BuildVersion 10.0.14393.1000
SerializationVersion 1.1.0.1
PSVersion 5.1.14393.1000
PSEdition Core
WSManStackVersion 3.0
CLRVersion
Activity
aburlak commentedon Dec 16, 2016
After some debugging, it looks like the root cause is that .NET Core's HttpClientHandler silently refuses to follow HTTPS-to-HTTP redirects.
However, there should be some way to at least look at the underlying response headers - currently, only a generic string description of the error is provided in the caught Exception (in desktop PowerShell - it is possible to retrieve the headers).
SteveL-MSFT commentedon Mar 15, 2017
Perhaps we should add a
-AllowRedirectToHTTPfromHTTPStype switch or just output a warning message[-]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects (Windows Server Nano)[/-][+]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects[/+]SteveL-MSFT commentedon Mar 15, 2017
This repros with alpha.17 on Ubuntu16 so not Nano specific
[-]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects[/-][+]Invoke-WebRequest / Invoke-RestMethod fails to follow HTTP redirects from HTTPS[/+]SteveL-MSFT commentedon Mar 15, 2017
@PowerShell/powershell-committee suggests
-allowInsecureRedirect34 remaining items
tsteven4 commentedon Jan 4, 2021
In the mean time:
C:\Windows\System32\curl -L -o outfile "https://example.com/somefile"
mkht commentedon Feb 7, 2021
This issue was a major obstacle for me when I migrated from PS5 to PS7.
I hope it will be addressed as soon as possible.
Fix Windows CI build, add Node.js v15
Fix Windows CI build, add Node.js v15
Fix Windows CI build, add Node.js v15