“too many automatic redirections were attempted” e

2019-02-08 01:09发布

问题:

I am attempting to request a page like "http://www.google.com/?q=random" using the webrequest class in vb.net. we are behind a firewall, so we have to authenticate our requests. I have gotten past the authentication part by adding my credentials. But once that works it seems to go into a redirecting loop.

Does anyone have an ideas, comments, suggetions why this is? Has anyone else experienced this problem?

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

回答1:

Make sure you have a cookie container setup.

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

You are probably not saving cookies and getting caught in a redirect loop.



回答2:

loHttp.AllowAutoRedirect = true

Instead of this, you have to use

loHttp.AllowAutoRedirect = False

to avoid error the error

"TOO MANY AUTOMATIC REDIRECTION WERE ATTEMPTED"



回答3:

I translated the C# that Darryl provided to VB and inserted it before the getResponse call.

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


回答4:

Maybe, you can individually process for each redirection by catch up Location from response and use suitable cookies.