小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

使用C#的HttpWebRequest模擬登陸訪問人人網(wǎng)

 franklinfj 2013-11-24

使用C#的HttpWebRequest模擬登陸訪問人人網(wǎng)

時間:2012-03-15 10:10來源:未知 作者:admin 點擊: 496次

無論使用任何語言做模擬登陸或者抓取訪問頁面,無外乎以下思路:
第一 啟用一個web訪問會話方法或者實例化一個web訪問類,如.net中的HttpWebRequest;
第二 模擬POST或者GET方式提交的數(shù)據(jù);
第三 模擬請求的頭;
第四 提交請求并獲得響應,及對響應做我們所需要的處理。
這里我們以人人網(wǎng)的登錄為例,將涉及到POST以及GET兩種請求方式。
在之前的文章《免費網(wǎng)頁抓包工具,火狐插件FireBug的抓包使用教程》中我們知道,登陸人人網(wǎng)的時候,一共做了一個POST請求以及兩個GET請求,如下圖:

人人網(wǎng)登錄請求

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

HttpWebRequest request = null;
  1. HttpWebResponse response = null;
  2. string gethost = string.Empty;
  3. CookieContainer cc = new CookieContainer();
  4. string Cookiesstr = string.Empty;
  5. try
  6. {
  7. //第一次POST請求
  8. string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&origURL=" + HostUrl + "&domain=renren.com";//模擬請求數(shù)據(jù),數(shù)據(jù)樣式可以用FireBug插件得到。人人網(wǎng)POST數(shù)據(jù)時,用戶名郵箱中的“@”變?yōu)椤?40”,所以我們也要作此變化
  9. string LoginUrl="http://www.renren.com/PLogin.do";
  10. request = (HttpWebRequest)WebRequest.Create(LoginUrl);//實例化web訪問類
  11. request.Method = "POST";//數(shù)據(jù)提交方式為POST
  12. //模擬頭
  13. request.ContentType = "application/x-www-form-urlencoded";
  14. byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
  15. request.ContentLength = postdatabytes.Length;
  16. //request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;
  17. request.AllowAutoRedirect = false;
  18. request.CookieContainer = cc;
  19. request.KeepAlive = true;
  20. //提交請求
  21. Stream stream;
  22. stream = request.GetRequestStream();
  23. stream.Write(postdatabytes, 0, postdatabytes.Length);
  24. stream.Close();
  25. //接收響應
  26. response = (HttpWebResponse)request.GetResponse();
  27. //保存返回cookie
  28. response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
  29. CookieCollection cook = response.Cookies;
  30. string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
  31. Cookiesstr = strcrook;
  32. //取第一次GET跳轉(zhuǎn)地址
  33. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  34. string content = sr.ReadToEnd();
  35. response.Close();
  36. string[] substr = content.Split(new char[] { '"' });
  37. gethost = substr[1];
  38. }
  39. catch (Exception)
  40. {
  41. //第一次POST出錯;
  42. }
HttpWebRequest request = null;
            HttpWebResponse response = null;
            string gethost = string.Empty;
            CookieContainer cc = new CookieContainer();
            string Cookiesstr = string.Empty;
            try
            {
            //第一次POST請求
            string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&origURL=" + HostUrl + "&domain=renren.com";//模擬請求數(shù)據(jù),數(shù)據(jù)樣式可以用FireBug插件得到。人人網(wǎng)POST數(shù)據(jù)時,用戶名郵箱中的“@”變?yōu)椤?40”,所以我們也要作此變化
            string  LoginUrl="http://www.renren.com/PLogin.do";
            request = (HttpWebRequest)WebRequest.Create(LoginUrl);//實例化web訪問類
            request.Method = "POST";//數(shù)據(jù)提交方式為POST
            //模擬頭
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
            request.ContentLength = postdatabytes.Length;
            //request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;
            request.AllowAutoRedirect = false;
            request.CookieContainer = cc;
            request.KeepAlive = true;
            //提交請求
            Stream stream;
            stream = request.GetRequestStream();
            stream.Write(postdatabytes, 0, postdatabytes.Length);
            stream.Close();
            //接收響應
            response = (HttpWebResponse)request.GetResponse();
            //保存返回cookie
            response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
            CookieCollection cook = response.Cookies;
            string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
            Cookiesstr = strcrook;
            //取第一次GET跳轉(zhuǎn)地址
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string content = sr.ReadToEnd();
            response.Close();
            string[] substr = content.Split(new char[] { '"' });
            gethost = substr[1];
            }
            catch (Exception)
            {
            //第一次POST出錯;
            }
            

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

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

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

GET與POST請求大同小異,這里便不再累述。三次請求結(jié)束,保存好你的cookie string,每次請求的時候都賦給請求的頭部,你就處于登錄狀態(tài)了。
人人網(wǎng)的HttpWebRequest登陸模擬很簡單,但是POST及GET涉及到了,是個不錯的案例。
當然,在.net想做自動訪問的操作還可以使用WebBrowser控件,而且還能夠和HttpWebRequest共用cookie,拋磚引玉一下不在本篇文章的討論范圍。

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多