天天看点

Too many automatic redirections were attempted 的解决方法

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(address); 

HttpWebRequest response = (HttpWebRequest)request.GetResponse(); 

Stream sm = response.GetResponseStream(); 

上面代码在访问一些网站时正常,但有一些却出错:

Too many automatic redirections were attempted

解决方法是加上一条语句,如下所示:

request.CookieContainer = new CookieContainer(); 

分析过程:

用IE访问”出错“网站,一切正常。这说明出错的应该是我的程序。

如果在request初始化后加入下面语句

reqest.AllowAutoRedirect = false; 

虽然request.GetResponse()正常返回,但是request.状态却是FOUND。

用WireShark抓包,分析IE与程序的数据包区别,发现在第2、3次跳转时IE多了Cookie。所以怀疑和它有关。

所以加上上面语句解决了问题。

本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/285483,如需转载请自行联系原作者

继续阅读