几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   C# (http://www.dimcax.com/hust/forumdisplay.php?f=147)
-   -   Too many automatic redirections were attempted 的解决方法 (http://www.dimcax.com/hust/showthread.php?t=33560)

huangyhg 2012-11-19 09:21 PM

Too many automatic redirections were attempted 的解决方法
 
  1. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(address);
  2. HttpWebRequest response = (HttpWebRequest)request.GetResponse();
  3. Stream sm = response.GetResponseStream();
上面代码在访问一些网站时正常,但有一些却出错:

Too many automatic redirections were attempted

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


  1. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(address);
  2. request.CookieContainer = new CookieContainer();
  3. HttpWebRequest response = (HttpWebRequest)request.GetResponse();
  4. Stream sm = response.GetResponseStream();

分析过程:

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

如果在request初始化后加入下面语句
  1. reqest.AllowAutoRedirect = false;
虽然request.GetResponse()正常返回,但是request.状态却是FOUND。


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

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


所有的时间均为北京时间。 现在的时间是 09:53 AM.