用HttpClient时发现一下页面跳转现象.

页面A要求授权自动跳转到页面B, 页面B进行了授权,在HTTP Header里要求SetCookie并跳转到页面A. 再次请求页面A的时候没有带上此Cookie信息, 页面A的服务器发现没有授权, 就去页面B进行授权. 导致了一个死循环.

最终会抛出一个”too many automatic redirections were attempted”的异常信息.

这个问题可参看http://stackoverflow.com/questions/518181/too-many-automatic-redirections-were-attempted-error-message-when-using-a-http 文章描述.

如果在.NET4.5上可以这么搞:

var baseAddress = new Uri("http://example.com");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("foo", "bar"),
new KeyValuePair<string, string>("baz", "bazinga"),
});
cookieContainer.Add(baseAddress, new Cookie("CookieName", "cookie_value"));
var result = client.PostAsync("/test", content).Result;
result.EnsureSuccessStatusCode();
}

但是如果低于4.5的版本, 就只能自己在HttpWebRequest对象上的搞了, 就像这样

Uri site = new Uri("http://www.google.com");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(site);
CookieContainer cookies = new CookieContainer();
request.CookieContainer = cookies; //Print out the number of cookies before the response (of course it will be blank)
Console.WriteLine(cookies.GetCookieHeader(site)); //Get the response and print out the cookies again
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Console.WriteLine(cookies.GetCookieHeader(site));
} Console.ReadKey();

too many automatic redirections were attempted的更多相关文章

  1. “重定向次数过多”或者“Too many automatic redirections were attempted”的错误:

    C# 代码 C# code? 1 2 3 4 5 6 7 8 9 String url="http://www.google.com.hk/search?hl=zh-CN&q=孟宪会 ...

  2. Automatic WordPress Updates Using FTP/FTPS or SSH

    Introduction When working with WordPress in a more secure environment where websites are not entirel ...

  3. the operation was attempted on an empty geometry Arcgis Project异常

    处理gis数据,投影变换时出现异常: the operation was attempted on an empty geometry 解决思路: arcgis的repair geometry方法:删 ...

  4. c# 传递Null的string值导致的调用C++的dll报错 Attempted to read or write protected memory.

    c# 调用C++的dll报错 Attempted to read or write protected memory:   原因是:c# 传递Null的string值导致的,将Null改为string ...

  5. APIPA(Automatic Private IP Addressing,自动专用IP寻址)

    APIPA APIPA(Automatic Private IP Addressing,自动专用IP寻址),是一个DHCP故障转移机制.当DHCP服务器出故障时, APIPA在169.254.0.1到 ...

  6. [SharePoint 2013] Automatic deployment script

    Implement automatic deployment through windows task. Add-PsSnapin Microsoft.SharePoint.PowerShell $t ...

  7. CentOS系统Kernel panic - not syncing: Attempted to kill init

    结果启动虚拟机出现如下问题: Kernel panic - not syncing: Attempted to kill init     解决方法: 系统启动的时候,按下'e'键进入grub编辑界面 ...

  8. JSONKit does not support Objective-C Automatic Reference Counting(ARC) / ARC forbids Objective-C objects in struct

    当我们在使用JSONKit处理数据时,直接将文件拉进项目往往会报这两个错“JSONKit   does not support Objective-C Automatic Reference Coun ...

  9. iOS开发 JSonKit does not support Objective-C Automatic Reference Counting(ARC)

    有使用JSonKit的朋友,如果遇到“JSonKit does not support Objective-C Automatic Reference Counting(ARC)”这种情况,可参照如下 ...

随机推荐

  1. UVA 1659 Help Little Laura 帮助小劳拉 (最小费用流,最小循环流)

    (同时也是HDU 2982,UVA的数据多) 题意:平面上有m条有向线段连接了n个点.你从某个点出发顺着有向线段行走,给走过的每条线段涂一种不同的颜色,最后回到起点.你可以多次行走,给多个回路涂色(要 ...

  2. Oracle RAC OCR 的备份与恢复

    Oracle Clusterware把整个集群的配置信息放在共享存储上,这些信息包括了集群节点的列表.集群数据库实例到节点的映射以及CRS应用程序资源信息.也即是存放在ocr 磁盘(或者ocfs文件) ...

  3. 请不要用SECONDS_BEHIND_MASTER来衡量MYSQL主备的延迟时间

    链接:http://www.woqutech.com/?p=1116 MySQL 本身通过 show slave status 提供了 Seconds_Behind_Master ,用于衡量主备之间的 ...

  4. 35、Android 性能优化、内存优化

    http://blog.csdn.net/a_asinceo/article/details/8222104 http://blog.csdn.net/a_asinceo/article/detail ...

  5. [Irving] Wpf DevexPress GridControl 获取选中行

    WPF前台绑定事件代码: <RelayAction TargetControl="{Binding ElementName=GCInstoragePart}" MethodN ...

  6. log4net 动态设定日志文件名

    参考文章: http://blog.csdn.net/haoxiaozigang1/article/details/16343303 通过这个篇文章的方法,只能修改文件的路径,文件名并没有修改 参考文 ...

  7. MDI端口和MDIX端口是什么? 又有什么作用?

    是网线的标准A类接法和B类接法.也就是人们通常所说的交叉网线和直联网线.直联网线就是 白黄 黄 白绿 蓝 白兰 绿 白棕 棕 另一端同样如此.交叉网线就是 另一端的1和3,2和6对调.这样就成了交叉网 ...

  8. 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)

    // 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...

  9. CreateThread函数&amp;&amp;CString::GetBuffer函数

    对这个两个常见的windows下的函数学习了一下: //最简单的创建多线程实例 #include <stdio.h> #include <windows.h> //子线程函数 ...

  10. Tkinter教程之Grid篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1813196 '''Tkinter教程之Grid篇'''# Tkinter参考中最推荐使用的一个 ...