powershell curl and WebRequest both follow redirects

人盡茶涼 提交于 2019-12-12 08:54:59

问题


I was trying to figure out the status code returned by my webpage, which most certainly is 301 (moved permanently), according to curl on the Linux subsystem, but using curl or the WebRequest object on powershell, it returns 200 (OK).

Why is this?


回答1:


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.

To turn off automatic redirection via WebRequest:

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

or Invoke-WebRequest cmdlet:

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

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

curl -L google.com


来源:https://stackoverflow.com/questions/41156458/powershell-curl-and-webrequest-both-follow-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!