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。所以怀疑和它有关。

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










本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/285483,如需转载请自行联系原作者
目录
相关文章
|
2月前
|
SQL Java
flywa报错Detected resolved migration not applied to database: 20221103.10000
flywa报错Detected resolved migration not applied to database: 20221103.10000
38 2
|
3月前
|
iOS开发 Perl
解决Xcode报错Stored properties cannot be marked unavailable with ‘@available‘
解决Xcode报错Stored properties cannot be marked unavailable with ‘@available‘
44 0
|
弹性计算 关系型数据库 MySQL
数据库表损坏解决方法( is marked as crashed and should be repaired)
表损坏原因过程大致如下,过了个国庆没有查看服务器状态,7号晚上手机收到ECS服务器异常短信,查了下发现是磁盘满了,导致无法创建临时空间,造成商城无法访问,速速的解决方法是删除了大量日志文件,访问网站发现已经ok了,一切正常,今天客户忽然打电话说,网站的搜索功能出现问题,我查了下错误原因
329 0
PowerDesigner在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
PowerDesigner在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
568 0
PowerDesigner在生成SQL时报错Generation aborted due to errors detected during the verification of the mod
|
Java API
解决办法:access restriction is not accessible due to restriction
解决办法:access restriction is not accessible due to restriction
127 0
automatic asynchronous creation if no note exists
Created by Wang, Jerry, last modified on May 12, 2015
126 0
automatic asynchronous creation if no note exists