我写的请求方法是这个:
string url = "http://wwww.baidu.com";
string cookie = "BAIDUID=FC5A3FE2E30531ABB846312A508CBB38:FG=1; BAIDUPSID=FC5A3FE2E30531ABB846312A508CBB38; H_PS_BABANNER=1; BD_HOME=0; H_PS_PSSID=8770_8229_1452_7801_9143_6504_6017_8592_9104_7799_9193_8974_9050_9024_9189; BD_UPN=123143";
string host = "www.baidu.com";
string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
string encoding = "gzip,deflate,sdch";
string language = "zh-CN,zh;q=0.8";
string cachecontrol = "max-age=0";
string connection = "keep-alive";
string referer = "http://baidu.com/";
string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36";
string ip = "119.75.217.56:80";
public HttpWebResponse GetHttpWebResponse(string url, string cookie, string host, string accept, string encoding, string userAgent, string referer)
{
System.GC.Collect();
if (string.IsNullOrEmpty(url))
{
url = this.url;
}
if (cookie == null)//不需要设置cookie的
{
}
if (cookie == "")//morendecookie
{
cookie = this.cookie;
}
if (string.IsNullOrEmpty(host))
{
host = this.host;
}
if (string.IsNullOrEmpty(accept))
{
accept = this.accept;
}
if (string.IsNullOrEmpty(userAgent))
{
userAgent = this.userAgent;
}
if (string.IsNullOrEmpty(referer))
{
referer = this.referer;
}
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
ServicePointManager.DefaultConnectionLimit = 200;
request.Timeout = 12000;
request.ServicePoint.Expect100Continue = false;
request.Host = host;
request.ProtocolVersion = HttpVersion.Version11;
request.Proxy = null;
request.Accept = accept;
//request.Address
request.Headers["Accept-Language"] = language;
request.Headers["Accept-Encoding"] = encoding;
request.Headers["Cache-Control"] = cachecontrol;
//request.Connection = connection;
request.Headers["Cookie"] =cookie;
request.KeepAlive = false;
request.Method = "GET";
request.UserAgent = this.userAgent;
//request.Referer = this.referer;
HttpWebResponse res = request.GetResponse() as HttpWebResponse;
if (res.StatusCode == HttpStatusCode.RequestTimeout)
{
if (res != null)
{
res.Close();
}
else
{
request.ServicePoint.CloseConnectionGroup("default");
if (request != null)
{
request = null;
}
}
res = null;
}
else
{
}
return res;
}
我调用时候是这样的:
HttpWebResponse baidu= help.GetHttpWebResponse(null, "", null, null, null, null, null);
string cookie=baidu.Headers["Set-Cookie"]; 为何得不到值呢是null,请问为何?谢谢
你的代码,我稍微修改了一下。修改如下,正常拿到Cookie
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; namespace ConsoleApplication20 { class Program { static void Main(string[] args) { HttpWebResponse baidu = new Program().GetHttpWebResponse(null, "", null, null, null, null, null); string cookie = baidu.Headers["Set-Cookie"]; } string url = "http://www.baidu.com"; string cookie = "BAIDUID=FC5A3FE2E30531ABB846312A508CBB38:FG=1; BAIDUPSID=FC5A3FE2E30531ABB846312A508CBB38; H_PS_BABANNER=1; BD_HOME=0; H_PS_PSSID=8770_8229_1452_7801_9143_6504_6017_8592_9104_7799_9193_8974_9050_9024_9189; BD_UPN=123143"; string host = "www.baidu.com"; string accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; string encoding = "gzip,deflate,sdch"; string language = "zh-CN,zh;q=0.8"; string cachecontrol = "max-age=0"; string connection = "keep-alive"; string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"; string ip = "119.75.217.56:80"; public HttpWebResponse GetHttpWebResponse(string url, string cookie, string host, string accept, string encoding, string userAgent, string referer) { System.GC.Collect(); url = this.url; cookie = this.cookie; host = this.host; accept = this.accept; userAgent = this.userAgent; CookieContainer cc = new CookieContainer(); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); request.CookieContainer = cc; ServicePointManager.DefaultConnectionLimit = 200; request.Timeout = 12000; request.ServicePoint.Expect100Continue = false; request.Host = host; request.ProtocolVersion = HttpVersion.Version11; request.Proxy = null; request.Accept = accept; request.Headers["Accept-Language"] = language; request.Headers["Accept-Encoding"] = encoding; request.Headers["Cache-Control"] = cachecontrol; request.Headers["Cookie"] = cookie; request.KeepAlive = false; request.Method = "GET"; request.UserAgent = this.userAgent; HttpWebResponse res = request.GetResponse() as HttpWebResponse; CookieCollection cook = res.Cookies; if (res.StatusCode == HttpStatusCode.RequestTimeout) { if (res != null) { res.Close(); } else { request.ServicePoint.CloseConnectionGroup("default"); if (request != null) { request = null; } } res = null; } else { } return res; } } }