HttpWebRequest.AllowAutoRedirect 屬性

定義

取得或設定值,這個值指出要求是否應該緊接在重新導向回應之後。Gets or sets a value that indicates whether the request should follow redirection responses.

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

屬性值

Boolean

如果要求應該自動追蹤來自網際網路資源的重新導向回應,則為 true;否則為 falsetrue if the request should automatically follow redirection responses from the Internet resource; otherwise, false. 預設值是 trueThe default value is true.

範例

下列程式碼範例會使用 AllowAutoRedirect 屬性,以允許要求遵循重新導向回應。The following code example uses the AllowAutoRedirect property to allow the request to follow redirection responses.

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

備註

AllowAutoRedirect true 如果您想要要求自動遵循 HTTP 重新導向標頭到資源的新位置,請設定為。Set AllowAutoRedirect to true if you want the request to automatically follow HTTP redirection headers to the new location of the resource. 要遵循的重新導向數目上限是由屬性所設定 MaximumAutomaticRedirectionsThe maximum number of redirections to follow is set by the MaximumAutomaticRedirections property.

如果 AllowAutoRedirect 設定為 false ,則 HTTP 狀態碼從300到399的所有回應都會傳回給應用程式。If AllowAutoRedirect is set to false, all responses with an HTTP status code from 300 to 399 is returned to the application.

系統會清除自動重新導向上的 Authorization 標頭,並 HttpWebRequest 自動嘗試重新驗證重新導向的位置。The Authorization header is cleared on auto-redirects and HttpWebRequest automatically tries to re-authenticate to the redirected location. 實際上,這表示如果可能會遇到重新導向,應用程式就無法將自訂驗證資訊放入授權標頭中。In practice, this means that an application can't put custom authentication information into the Authorization header if it is possible to encounter redirection. 相反地,應用程式必須執行並註冊自訂驗證模組。Instead, the application must implement and register a custom authentication module. System.Net.AuthenticationManager和相關的類別是用來執行自訂驗證模組。The System.Net.AuthenticationManager and related class are used to implement a custom authentication module. AuthenticationManager.Register方法會註冊自訂驗證模組。The AuthenticationManager.Register method registers a custom authentication module.

適用於