powershell curl和WebRequest都遵循重定向

时间:2016-12-15 04:30:19

标签: powershell curl webrequest

我试图弄清楚我的网页返回的状态代码,根据Linux子系统的卷曲,肯定是301(永久移动),但是在powershell上使用curl或WebRequest对象,它返回200(OK) )。

为什么会这样?

1 个答案:

答案 0 :(得分:11)

这是因为.NET和PowerShell默认遵循重定向,但curl不会这样做。默认值$request = [System.Net.WebRequest]::Create("http://google.com") $request.AllowAutoRedirect = $false $request.GetResponse() 为true,Invoke-WebRequest的MaximumRedirection默认值为5.

通过WebRequest关闭自动重定向:

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

或Invoke-WebRequest cmdlet:

curl -L google.com

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

Compare-Object