且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

powershell curl和WebRequest都遵循重定向

更新时间:2023-11-19 13:30:52

这是因为。 NET和PowerShell在默认情况下遵循重定向,但是curl不这样做。 HttpWebRequest.AllowAutoRedirect 的默认值为true,而Invoke-WebRequest的MaximumRedirection默认值为5。

It is because .NET and PowerShell are following redirects by default but curl does not do this. The default value of HttpWebRequest.AllowAutoRedirect is true and Invoke-WebRequest's MaximumRedirection default value is 5.

要关闭通过WebRequest自动重定向:

To turn off automatic redirection via WebRequest:

$request = [System.Net.WebRequest]::Create("http://google.com")
$request.AllowAutoRedirect = $false
$request.GetResponse()

或Invoke-WebRequest cmdlet:

or Invoke-WebRequest cmdlet:

Invoke-WebRequest -Uri "http://google.com" -MaximumRedirection 0

或者,使用-L标志跟随curl中的重定向:

Alternatively, use the -L flag to follow redirects in curl:

curl -L google.com