“重定向次数过多”或者“Too many automatic redirections were attempted”的错误:
C# 代码
1
2
3
4
5
6
7
8
9
|
String url=
"http://www.google.com.hk/search?hl=zh-CN&q=孟宪会"
;
System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
r.AllowAutoRedirect =
true
;
//System.Net.CookieContainer c = new System.Net.CookieContainer();
//r.CookieContainer = c;
System.Net.HttpWebResponse res = r.GetResponse()
as
System.Net.HttpWebResponse;
System.IO.StreamReader s =
new
System.IO.StreamReader(res.GetResponseStream(),System.Text.Encoding.GetEncoding(
"GB2312"
));
Response.Write(s.ReadToEnd());
res.Close();
|
解决方法就是加上CookieContainer,完整的代码:
C# 代码
1
2
3
4
5
6
7
8
9
|
String url=
"http://www.google.com.hk/search?hl=zh-CN&q=孟宪会"
;
System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
r.AllowAutoRedirect = true ;
System.Net.CookieContainer c = new System.Net.CookieContainer();
r.CookieContainer = c;
System.Net.HttpWebResponse res = r.GetResponse()
as
System.Net.HttpWebResponse;
System.IO.StreamReader s =
new
System.IO.StreamReader(res.GetResponseStream(),System.Text.Encoding.GetEncoding(
"GB2312"
));
Response.Write(s.ReadToEnd());
res.Close();
|