http://www.aygfsteel.com/ebecket 返還網
          隨筆-140  評論-11  文章-131  trackbacks-0

          實現HTTP內容的抓取

          前段時間做了一個網頁爬蟲,初次接觸,收獲了很多知識。其中關于HTTP協議的內容,記述如下:

                  RFC2616中主要描述了HTTP 1.1協議。下面的描述沒有實現其各個方面的內容,只提出了一種能夠完成所有HTTP網頁抓取的最小實現(不能夠抓取HTTPS)。

                  1、首先提交一個URL地址,分為普通的GET網頁獲取,POST的數據提交兩種基本模式。

          建立HttpWebReques實例,其中uri是網頁的URL的地址:
             HttpWebRequest webrequest = (HttpWebRequest) WebRequest.Create(uri);

          KeepAlive表示HTTP的連接是長連接:
             webrequest.KeepAlive = true;

          如果需要,添加引用地址,主要用于防止其他網站的連接引用,比如登陸時,經常需要驗證:
             if(referer!=null)
             {
              webrequest.Referer=referer;
             }

          選擇數據的提交方式,有GET、POST兩種方式,HEAD不常用:
             switch(RequestMethod)
             {
              case 1:
               webrequest.Method="GET";
               break;
              case 2:
               webrequest.Method="POST";
               break;
              case 3:
               webrequest.Method="HEAD";
               break;
              default:
               webrequest.Method="GET";
               break;
             }

          設置User-Agent,經常遇到,在某些網站中,做了限制,User-Agent為空,則不能訪問:
             webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50215; fqSpider)";

          添加其他的HTTP的Header信息,collHeader是一個NameValue的Collection:
             if(collHeader!=null&&collHeader.Count>0)
             {
              int iCount = collHeader.Count;
              string key;
              string keyvalue;
           
              for (int i=0; i < iCount; i++)
              {
               key = collHeader.Keys[i];
               keyvalue = collHeader[i];
               webrequest.Headers.Add(key, keyvalue);
              }
             }

          設置Content-Type的內容,如果為POST,設置成application/x-www-form-urlencoded,如果是Get設置成text/html:
             if(webrequest.Method=="POST")
             {
              webrequest.ContentType="application/x-www-form-urlencoded";
             }
             else
             {
              webrequest.ContentType = "text/html";
             }
             
           
          設置代理服務器地址和端口:
             if ((ProxyServer!=null) &&(ProxyServer.Length > 0))
             {
              webrequest.Proxy = new
               WebProxy(ProxyServer,ProxyPort);
             }

          設置是否允許自動轉移:
             webrequest.AllowAutoRedirect = true;

          設置基本的登陸認證 :
             if (NwCred)
             {
              CredentialCache wrCache =
               new CredentialCache();
              wrCache.Add(new Uri(uri),"Basic",
               new NetworkCredential(UserName,UserPwd));
              webrequest.Credentials = wrCache;
             }  

          設置Request的Cookie容器:
             webrequest.CookieContainer=Cookies;

          設置POST數據:
             byte[] bytes = Encoding.ASCII.GetBytes(RequestData);
             webrequest.ContentLength=bytes.Length;
           
             Stream oStreamOut = webrequest.GetRequestStream();
             oStreamOut.Write(bytes,0,bytes.Length);
             oStreamOut.Close();

          posted on 2010-01-20 01:30 becket_zheng 閱讀(451) 評論(0)  編輯  收藏 所屬分類: 網頁web前端技術C#
          主站蜘蛛池模板: 定陶县| 衡南县| 通许县| 林甸县| 增城市| 平定县| 遂平县| 临猗县| 平武县| 六盘水市| 元氏县| 通州市| 松溪县| 淮阳县| 旌德县| 卫辉市| 商丘市| 罗定市| 鹿邑县| 临猗县| 府谷县| 驻马店市| 灵石县| 九寨沟县| 双峰县| 涿州市| 郑州市| 会宁县| 深圳市| 温宿县| 高唐县| 治多县| 梨树县| 开平市| 彰化县| 准格尔旗| 密山市| 三门县| 五河县| 肇源县| 天镇县|