在 .NET 中使用 httpWebRequest 时出现“尝试了太多自动重定向”错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/518181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"too many automatic redirections were attempted" error message when using a httpWebRequest in .NET
提问by tooleb
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.
我正在尝试使用 vb.net 中的 webrequest 类请求像“ http://www.google.com/?q=random”这样的页面。我们在防火墙后面,所以我们必须验证我们的请求。通过添加我的凭据,我已经通过了身份验证部分。但是一旦成功,它似乎进入了重定向循环。
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()
回答by Darryl Braaten
Make sure you have a cookie container setup.
确保您有一个 cookie 容器设置。
CookieContainer cookieContainer = new CookieContainer();
loHttp.CookieContainer = cookieContainer;
You are probably not saving cookies and getting caught in a redirect loop.
您可能没有保存 cookie 并陷入重定向循环。
回答by Dhivya
loHttp.AllowAutoRedirect = true
Instead of this, you have to use
而不是这个,你必须使用
loHttp.AllowAutoRedirect = False
to avoid error the error
避免错误错误
"TOO MANY AUTOMATIC REDIRECTION WERE ATTEMPTED"
“尝试了太多自动重定向”
回答by tooleb
I translated the C# that Darryl provided to VB and inserted it before the getResponse call.
我翻译了 Darryl 提供给 VB 的 C# 并将其插入到 getResponse 调用之前。
Dim cookieContainer As CookieContainer = New CookieContainer()
loHttp.CookieContainer = cookieContainer
loWebResponse = loHttp.GetResponse()
回答by Tran Thanh
Maybe, you can individually process for each redirection by catch up Location from response and use suitable cookies.
也许,您可以通过从响应中获取位置并使用合适的 cookie 来单独处理每个重定向。

