首页 新闻 会员 周边 捐助

HttpWebRespose得不到cookie

-1
悬赏园豆:10 [已解决问题] 解决于 2015-07-03 10:48

我写的请求方法是这个:

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,请问为何?谢谢

秋水惜朝的主页 秋水惜朝 | 初学一级 | 园豆:4
提问于:2014-10-24 10:41
< >
分享
最佳答案
0

你的代码,我稍微修改了一下。修改如下,正常拿到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;
        }
    }
}
收获园豆:10
需要格局 | 老鸟四级 |园豆:2145 | 2014-10-24 11:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册