几何尺寸与公差论坛

 找回密码
 注册
查看: 1610|回复: 0

Too many automatic redirections were attempted 的解决方法

[复制链接]
发表于 2012-11-19 21:21:18 | 显示全部楼层 |阅读模式

  • HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(address);
  • HttpWebRequest response = (HttpWebRequest)request.GetResponse();
  • Stream sm = response.GetResponseStream();
上面代码在访问一些网站时正常,但有一些却出错:

Too many automatic redirections were attempted

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

  


  • HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(address);
  • request.CookieContainer = new CookieContainer();
  • HttpWebRequest response = (HttpWebRequest)request.GetResponse();
  • Stream sm = response.GetResponseStream();
  

分析过程:

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

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


  • reqest.AllowAutoRedirect = false;
虽然request.GetResponse()正常返回,但是request.状态却是FOUND。


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

所以加上上面语句解决了问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-4-27 15:13 , Processed in 0.035351 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表