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

屬性值

如果要求應該自動追蹤來自網際網路資源的重新導向回應,則為 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 自動嘗試重新驗證至重新導向的位置。 實際上,這表示如果可能遇到重新導向,應用程式就無法將自訂驗證資訊放入授權標頭中。 相反地,應用程式必須實作並註冊自訂驗證模組。 System.Net.AuthenticationManager和相關類別可用來實作自訂驗證模組。 方法 AuthenticationManager.Register 會註冊自訂驗證模組。

適用於