2009-02-05 18 views
18

我正嘗試使用vb.net中的webrequest類請求類似「http://www.google.com/?q=random」的頁面。 我們在防火牆後面,所以我們必須驗證我們的請求。 我已經通過添加我的憑證過去了身份驗證部分。 但是一旦有效,它似乎進入重定向循環。在.NET中使用httpWebRequest時出現「嘗試過多自動重定向」的錯誤消息

有沒有人有想法,意見,建議爲什麼這是? 有沒有其他人遇到過這個問題?

Dim loHttp As HttpWebRequest = CType(WebRequest.Create(_url), HttpWebRequest) 
loHttp.Timeout = 10000 
loHttp.Method = "GET" 
loHttp.KeepAlive = True 
loHttp.AllowAutoRedirect = True 
loHttp.PreAuthenticate = True 
Dim _cred1 As NetworkCredential = ... //this is setup 
//snip out this stuff 
loHttp.Credentials = _cc 
loWebResponse = loHttp.GetResponse() 

回答

41

確保您有cookie容器設置。

CookieContainer cookieContainer = new CookieContainer(); 
loHttp.CookieContainer = cookieContainer; 

您可能沒有保存cookie並被重定向循環所捕獲。

+0

是的,這是完全是對的。謝謝。 – tooleb 2009-02-05 22:31:36

+0

這讓我從痛苦和痛苦中解脫出來!非常感謝! :D – Maritim 2013-05-17 22:19:20

2

我翻譯了Darryl提供給VB的C#,並將其插入到getResponse調用之前。

Dim cookieContainer As CookieContainer = New CookieContainer() 
loHttp.CookieContainer = cookieContainer 
loWebResponse = loHttp.GetResponse() 
0

也許,你可以單獨處理每個重定向的追趕位置從響應和使用合適的cookie。

7
loHttp.AllowAutoRedirect = true 

取而代之的是,你必須使用

loHttp.AllowAutoRedirect = False 

,以避免錯誤的錯誤

「太多的自動重定向已嘗試」

相關問題