powershell curl and WebRequest both follow redirects

后端 未结 1 875
孤独总比滥情好
孤独总比滥情好 2021-02-14 11:17

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 t

1条回答
  •  感情败类
    2021-02-14 12:01

    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
    

    0 讨论(0)
提交回复
热议问题