HttpWebRequest.AllowAutoRedirect 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,這個值指出要求是否應該緊接在重新導向回應之後。
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 自動嘗試重新驗證至重新導向的位置。 實際上,這表示如果可能遇到重新導向,應用程式就無法將自定義驗證資訊放入 Authorization 標頭中。 相反地,應用程式必須實作並註冊自定義驗證模組。 System.Net.AuthenticationManager和相關類別可用來實作自定義驗證模組。 方法 AuthenticationManager.Register 會註冊自定義驗證模組。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應