HttpWebRequest.AllowAutoRedirect 属性

定义

获取或设置一个值,该值指示请求是否应跟随重定向响应。

public:
 virtual property bool AllowAutoRedirect { bool get(); void set(bool value); };
public:
 property bool AllowAutoRedirect { bool get(); void set(bool value); };
public virtual bool AllowAutoRedirect { get; set; }
public bool AllowAutoRedirect { get; set; }
member this.AllowAutoRedirect : bool with get, set
Public Overridable Property AllowAutoRedirect As Boolean
Public Property AllowAutoRedirect As Boolean

属性值

如果请求应自动遵循来自 Internet 资源的重定向响应,则为 true;否则为 false。 默认值是 true

示例

下面的代码示例使用 AllowAutoRedirect 属性允许请求遵循重定向响应。

// Create a new HttpWebRequest Object to the mentioned URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.contoso.com" ) );
myHttpWebRequest->MaximumAutomaticRedirections = 1;
myHttpWebRequest->AllowAutoRedirect = true;
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
// Create a new HttpWebRequest Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");	
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();

     'This method creates a new HttpWebRequest Object to the mentioned URL.
         Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
         myHttpWebRequest.MaximumAutomaticRedirections = 1
         myHttpWebRequest.AllowAutoRedirect = True
         Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

注解

true如果希望请求自动遵循 HTTP 重定向标头到资源的新位置,请将 设置为 AllowAutoRedirect 。 要遵循的最大重定向数由 MaximumAutomaticRedirections 属性设置。

如果 AllowAutoRedirect 设置为 false,则 HTTP 状态代码为 300 到 399 的所有响应将返回到应用程序。

授权标头在自动重定向时被清除,并 HttpWebRequest 自动尝试对重定向的位置重新进行身份验证。 实际上,这意味着如果应用程序可能遇到重定向,则无法将自定义身份验证信息放入 Authorization 标头。 相反,应用程序必须实现并注册自定义身份验证模块。 System.Net.AuthenticationManager和 相关类用于实现自定义身份验证模块。 方法 AuthenticationManager.Register 注册自定义身份验证模块。

适用于