1
我试图找出我的网页返回的状态代码,它肯定是301(永久移动),根据Linux子系统上的卷曲,但使用curl或WebRequest对象powershell,它返回200(确定)。powershell curl和WebRequest都遵循重定向
这是为什么?
我试图找出我的网页返回的状态代码,它肯定是301(永久移动),根据Linux子系统上的卷曲,但使用curl或WebRequest对象powershell,它返回200(确定)。powershell curl和WebRequest都遵循重定向
这是为什么?
这是因为.NET和PowerShell默认遵循重定向,但curl不会这样做。的HttpWebRequest.AllowAutoRedirect
默认值为真和Invoke-的WebRequest的MaximumRedirection默认值为5
要通过的WebRequest关闭自动重定向:
$request = [System.Net.WebRequest]::Create("http://google.com")
$request.AllowAutoRedirect = $false
$request.GetResponse()
或Invoke-WebRequest的小命令:
Invoke-WebRequest -Uri "http://google.com" -MaximumRedirection 0
替代地,使用-L标志跟随卷曲中的重定向:
curl -L google.com