PowerShell curl和WebRequest都遵循重定向



我试图找出我的网页返回的状态代码,该网页肯定是301(永久移动),但使用Linux子系统上的Curl,但使用Curl或WebRequest对象在PowerShell上,它返回200(OK)。

为什么这是?

这是因为.net和powerShell默认情况下遵循重定向,但curl不执行此操作。HttpWebRequest.AllowAutoRedirect的默认值是真实的,Indoke-webrequest的最大值默认值为5。

通过WebRequest关闭自动重定向:

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

或Indoke-webrequest cmdlet:

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

或者,使用-l标志在卷发中遵循重定向:

curl -L google.com

最新更新