使用C#的HttpWebRequest模擬登錄訪問人人網

使用任何語言作模擬登錄或者抓取訪問頁面,無外乎如下思路:web

第一 啓用一個web訪問會話方法或者實例化一個web訪問類,如.net中的HttpWebRequest;
第二 模擬POST或者GET方式提交的數據;
第三 模擬請求的頭;
第四 提交請求並得到響應,及對響應作咱們所須要的處理。
這裏咱們以人人網的登陸爲例,將涉及到POST以及GET兩種請求方式。
你們使用抓包工具(IE調試工具/httpwatch)都是能夠的,我這裏採用httpwatch,登錄人人網的時候(www.renren.com),一共作了一個POST請求以及兩個GET請求,以下圖:服務器

post了一個後,第一個返回狀態值是200的通常就是登陸後的首頁地址,有些網頁須要跳轉的比較多一些,可是方法都是同樣的,cookie

觀察這三個請求的詳細信息,不難看出這裏都是順序的,第一個GET請求的地址由POST的響應獲得,而第二個GET請求的地址又由第一個GET的響應獲得。app

每次請求與下一次請求之間的聯繫就是每次請求後返回的Cookies數據,前一次的返回Cookie數據須要同下一次請求一同發送到服務器,這也是C#模擬網站登錄的關鍵。dom

這裏須要注意幾點:工具

1、選擇須要post的地址,能夠經過工具查看得到,也能夠經過查看網頁源代碼得到。post

2、content能夠查看返回的內容,或者是包含下一跳的連接地址。到最後必定是首頁的網頁內容。網站

先來模擬第一個POST請求google

  1. HttpWebRequest request = null;   
  2. HttpWebResponse response = null;   
  3. string gethost = string.Empty;   
  4. CookieContainer cc = new CookieContainer();   
  5. string Cookiesstr = string.Empty;   
  6. try  
  7. {   
  8.         //第一次POST請求   
  9.     string postdata =「」email=adm13956587&password=786954887&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&captcha_type=web_login" //模擬請求數據,httpwatch中點擊stream,就能夠直接複製了
  10.     string  LoginUrl="http://www.renren.com/PLogin.do";   
  11.       request = (HttpWebRequest)WebRequest.Create(LoginUrl);//實例化web訪問類   
  12.     request.Method = "POST";//數據提交方式爲POST   
  13.       //模擬頭   
  14.     request.ContentType = "application/x-www-form-urlencoded";   
  15.       byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);   
  16.       request.ContentLength = postdatabytes.Length;   
  17.   
  18.       request.AllowAutoRedirect = false;   
  19.       request.CookieContainer = cc;   
  20.       request.KeepAlive = true;   
  21.       //提交請求   
  22.     Stream stream;   
  23.       stream = request.GetRequestStream();   
  24.       stream.Write(postdatabytes, 0, postdatabytes.Length);   
  25.       stream.Close();   
  26.       //接收響應   
  27.     response = (HttpWebResponse)request.GetResponse();   
  28.       //保存返回cookie   
  29.       response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);   
  30.       CookieCollection cook = response.Cookies;   
  31.       string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);   
  32.       Cookiesstr = strcrook;   
  33.       //從返回的stream當中取第一次GET跳轉地址: The URL has moved <a href="http://www.renren.com/home">here</a>
  34.     StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);   
  35.       string content = sr.ReadToEnd();   
  36.       response.Close();   
  37.       string[] substr = content.Split(new char[] { '"' });   
  38.       gethost = substr[1];   //http://www.renren.com/home
  39. }   
  40. catch (Exception)   
  41. {   
  42.       //第一次POST出錯;   
  43. }  

註釋寫的很詳細了,在這就再也不分析,也許有人對request = (HttpWebRequest)WebRequest.Create(LoginUrl)有疑問,能夠去google一下HttpWebRequest和WebRequest的區別,簡單來講WebRequest是一個抽象類,不能直接實例化,須要被繼承,而HttpWebRequest繼承自WebRequest。url

再模擬第一個和第二個GET請求

  1. try  
  2. {   
  3.     request = (HttpWebRequest)WebRequest.Create(gethost);   
  4.     request.Method = "GET";   
  5.     request.KeepAlive = true;   
  6.     request.Headers.Add("Cookie:" + Cookiesstr);   
  7.     request.CookieContainer = cc;   
  8.     request.AllowAutoRedirect = false;   
  9.     response = (HttpWebResponse)request.GetResponse();   
  10.     //設置cookie   
  11.     Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);   
  12.     //取再次跳轉連接   The URL has moved <a href="http://www.renren.com/1915651750">here</a>
  13.     StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);   
  14.     string ss = sr.ReadToEnd();   
  15.     string[] substr = ss.Split(new char[] { '"' });   
  16.     gethost = substr[1];   //http://www.renren.com/1915651750
  17.     request.Abort();   
  18.     sr.Close();   
  19.     response.Close();   
  20. }   
  21. catch (Exception)   
  22. {   
  23.     //第一次GET出錯   
  24. }   
  25. try  
  26. {   
  27.     //第二次GET請求   
  28.     request = (HttpWebRequest)WebRequest.Create(gethost);   
  29.     request.Method = "GET";  
  30.     request.KeepAlive = true;  
  31.     request.Headers.Add("Cookie:" + Cookiesstr);   
  32.     request.CookieContainer = cc;   
  33.     request.AllowAutoRedirect = false;   
  34.     response = (HttpWebResponse)request.GetResponse();   
  35.     //設置cookie   
  36.     Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
  37. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

  38.  

    string ss = sr.ReadToEnd();

  39. webBrowser1.Navigate("about:blank");
  40. webBrowser1.Document.OpenNew(true);

  41. webBrowser1.Document.Write(ss);

  42.     request.Abort();   
  43.     response.Close();   
  44. }   
  45. catch (Exception)   
  46. {   
  47.     //第二次GET出錯   
  48. }  

GET與POST請求大同小異,這裏便再也不累述。三次請求結束,保存好你的cookie string,每次請求的時候都賦給請求的頭部,你就處於登陸狀態了。

相關文章
相關標籤/搜索