用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. jQuery 遍历 - parent() 方法

    ylbtech-jQuery-sizzle:jQuery 遍历 - parent() 方法  parent() 获得当前匹配元素集合中每个元素的父元素,使用选择器进行筛选是可选的. 1.A,jQuer ...

  2. 转载:C语言的谜题

    转载:http://coolshell.cn/articles/945.html 这几天,本站推出了几篇关于C语言的很多文章如下所示: 语言的歧义 [酷壳链接] [CSDN链接] 谁说C语言很简单? ...

  3. Codeforces 167B Wizards and Huge Prize(概率dp)

    题意: n个人,开始有一个容量为k得背包,击败一个人背包可以获得一定容量或得到一个财富(放入背包内),给出击败每个人的概率,求至少击败l个人,且背包容量大于获得的总财富值的概率 分析: 状态好确定,d ...

  4. UILabel 自适应大小

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init]; paragraphStyle.lineB ...

  5. U盘安装Centos5.3

    一.制作 U 盘启动引导盘 1. 插上 U 盘,打开 UltraISO 软件,打开CentOS-5.3-i386-bin-DVD.iso 文件: 2.点启动--写入硬盘镜像,在硬盘驱动器里面选择你的 ...

  6. csv文件与DataTable互相导入处理

    封装处理下,以后项目用到可以直接使用,比较简单. 1.首先看封装好的类 using System; using System.Data; using System.IO; using System.T ...

  7. rfid 门卡系统和人体红外感应开发

    今天忙了一天了,因为毕昇杯我发现如果不加把劲,可能寒假之前代码搞不出了,今天突击了两个模块,一个人体感应模块,和rfid刷卡模块,这两个模块谈不上自己编写代码,今天的任务也仅仅是看懂了代码,现在我总结 ...

  8. 关于webpack最好的文档

    这几天研究webpack打包工具,在网上搜了无数的资料,鱼龙混杂.看了几十份资料,依然没有一个可以完整的描述的. 折腾了那么久,还是放弃治疗了.回到官网,一字一句的阅读,一个小时就彻底明白了. 学习新 ...

  9. Leetcode Largest Number c++ solution

    Total Accepted: 16020 Total Submissions: 103330     Given a list of non negative integers, arrange t ...

  10. bzoj 3732 Network(最短路+倍增 | LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3732 [题意] 给定一个无向图,处理若干询问:uv路径上最长的边最小是多少? [思路一 ...