.net 数据抓取中添加cookie值

下面是转载:http://www.cnblogs.com/duohahawuwu/archive/2011/04/20/2021975.html

最近发现抓取网页数据的工作越来越多了,抓取技术,正则表达式,各种麻烦,这次又遇到了cookie的问题。

首先痛恨一下那些靠cookie来变换网页内容的网站,嘿嘿

 在网上找了一些资料,最后整理出来了结论

 说结论之前先说一下cookie的层次关系,在.net中cookie的大小关系如下CookieContainer--CookieCollection--Cookie  ,其中我们使用的HttpWebRequest使用的正式第一级别也就是CookieContainer,弄明白这个关系以后就可以进行相应的代码开发了

public void StartClimb()
         {
             string urlContent = "";
 
             try
             {
                 CookieCollection cookies = new CookieCollection();
                 cookies.Add(new Cookie("cookie名称", "cookie值"));
 
                 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.XXXXX.com");
                 webRequest.Timeout = 30000;
                 webRequest.AllowAutoRedirect = true;
                 webRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
                 webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                 webRequest.Headers.Set("Pragma", "no-cache");
                 webRequest.Headers.Add("Accept-Language", "zh-cn,zh;q=0.5");
                 webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
                 webRequest.Headers.Add("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
 
                 CookieContainer myCookieContainer = new CookieContainer();
                 myCookieContainer.Add(webRequest.RequestUri,cookies);
                 webRequest.CookieContainer = myCookieContainer;
 
                 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                 
 
                 Stream stream;
                 //对gzip和deflate进行解密处理
                 switch (webResponse.ContentEncoding)
                 {
                     case "gzip":
                         stream = new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress);
                         break;
                     case "deflate":
                         stream = new DeflateStream(webResponse.GetResponseStream(), CompressionMode.Decompress);
                         break;
                     default:
                         stream = webResponse.GetResponseStream();
                         break;
                 }
 
                 StreamReader streamReader;
                 streamReader = new StreamReader(stream, Encoding.UTF8);
                 //streamReader = new StreamReader(stream, Encoding.Default);
                 //streamReader = new StreamReader(stream, Encoding.UTF8);
                 urlContent = streamReader.ReadToEnd();
                 streamReader.Close();
                 stream.Close();
 
                 if (webRequest != null) { webRequest.Abort(); }
                 if (webResponse != null) { webResponse.Close(); }
             }
             catch (WebException ex)
             {
 
             }
             catch (Exception ex)
             {
 
             }
 
             
         }

有了上面的代码应该就可以搞定cookie的事情了。不过一般我们在开发的时候可能会忘记Cookie的Domain问题,这样的话在程序执行的时候会导致报错 

 

myCookieContainer.Add(webRequest.RequestUri,cookies);

这句话声明了必要的Domain,CSDN上面的一个帖子的问题就出在于此http://topic.csdn.net/u/20080402/18/d63636a7-d81e-40f2-b972-b9c6bb775ac4.html

 

 

对了,如果你不知道需要传递什么cookie给服务器,那么有很多办法,其中最简单的就是通过firefox的firebig来查看,还有ie9里面的f12功能,再有就是自己写程序,在抓取的时候获取一些传回的cookie值,但是记得也要声明URL

 

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
CookieCollection CCol = myCookieContainer.GetCookies(webRequest.RequestUri);

posted @ 2012-12-03 11:42  金河  阅读(852)  评论(0编辑  收藏  举报