powershell curl和WebRequest都遵循重定向 [英] powershell curl and WebRequest both follow redirects

查看:96
本文介绍了powershell curl和WebRequest都遵循重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

这是为什么?

推荐答案

这是因为。 NET和PowerShell在默认情况下遵循重定向,但是curl不这样做。 HttpWebRequest.AllowAutoRedirect 的默认值为true,而Invoke-WebRequest的MaximumRedirection默认值为5。

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.

要关闭通过WebRequest自动重定向:

To turn off automatic redirection via WebRequest:

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

或Invoke-WebRequest cmdlet:

or Invoke-WebRequest cmdlet:

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

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

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

curl -L google.com

这篇关于powershell curl和WebRequest都遵循重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆