使用C#的HttpWebRequest類比登陸訪問人人網(轉)

來源:互聯網
上載者:User

標籤:

無論使用任何語言做類比登陸或者抓取訪問頁面,無外乎以下思路:
第一 啟用一個web訪問會話方法或者執行個體化一個web訪問類,如.net中的HttpWebRequest;
第二 類比POST或者GET方式提交的資料;
第三 類比請求的頭;
第四 提交請求並獲得響應,及對響應做我們所需要的處理。
這裡我們以人人網的登入狀態例,將涉及到POST以及GET兩種請求方式。
在之前的文章《免費網頁抓包工具,Firefox外掛程式FireBug的抓包使用教程》中我們知道,登陸人人網的時候,一共做了一個POST請求以及兩個GET請求,如:

觀察這三個請求的詳細資料,不難看出第一個GET請求的地址可以由POST的響應得到,而第二個GET請求的地址又由第一個GET的響應得到。
先來類比第一個POST請求

view plainprint?
  1. HttpWebRequest request = null;  
  2. HttpWebResponse response = null;  
  3. string gethost = string.Empty;  
  4. CookieContainer cc = new CookieContainer(); // 若要從遠程調用中擷取COOKIE一定要為request設定一個CookieContainer用來裝載返回的cookies
  5. string Cookiesstr = string.Empty;  
  6. try  
  7. {  
  8.         //第一次POST請求  
  9.     string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&origURL=" + HostUrl + "&domain=renren.com";//類比請求資料,資料樣式可以用FireBug外掛程式得到。人人網POST資料時,使用者名稱郵箱中的“@”變為“%40”,所以我們也要作此變化  
  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.       //request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;  
  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.       //取第一次GET跳轉地址  
  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];  
  39. }  
  40. catch (Exception)  
  41. {  
  42.       //第一次POST出錯;  
  43. }  

注釋寫的很詳細了,在這就不再分析,也許有人對request = (HttpWebRequest)WebRequest.Create(LoginUrl)有疑問,可以去google一下HttpWebRequest和WebRequest的區別,簡單來說WebRequest是一個抽象類別,不能直接執行個體化,需要被繼承,而HttpWebRequest繼承自WebRequest。

再類比第一個和第二個GET請求

view plainprint?
  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.     //取再次跳轉連結  
  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];  
  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.     request.Abort();  
  38.     response.Close();  
  39. }  
  40. catch (Exception)  
  41. {  
  42.     //第二次GET出錯  
  43. }  

GET與POST請求大同小異,這裡便不再累述。三次請求結束,儲存好你的cookie string,每次請求的時候都賦給請求的頭部,你就處於登入狀態了。
人人網的HttpWebRequest登陸類比很簡單,但是POST及GET涉及到了,是個不錯的案例。
當然,在.net想做自動訪問的操作還可以使用WebBrowser控制項,而且還能夠和HttpWebRequest共用cookie,拋磚引玉一下不在本篇文章的討論範圍。

使用C#的HttpWebRequest類比登陸訪問人人網(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.