久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

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

 franklinfj 2013-11-24

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

時(shí)間:2012-03-15 10:10來(lái)源:未知 作者:admin 點(diǎn)擊: 496次

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

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

觀察這三個(gè)請(qǐng)求的詳細(xì)信息,,不難看出第一個(gè)GET請(qǐng)求的地址可以由POST的響應(yīng)得到,,而第二個(gè)GET請(qǐng)求的地址又由第一個(gè)GET的響應(yīng)得到。
先來(lái)模擬第一個(gè)POST請(qǐng)求

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請(qǐng)求
  8. string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&origURL=" + HostUrl + "&domain=renren.com";//模擬請(qǐng)求數(shù)據(jù),,數(shù)據(jù)樣式可以用FireBug插件得到,。人人網(wǎng)POST數(shù)據(jù)時(shí),用戶(hù)名郵箱中的“@”變?yōu)椤?40”,所以我們也要作此變化
  9. string LoginUrl="http://www.renren.com/PLogin.do";
  10. request = (HttpWebRequest)WebRequest.Create(LoginUrl);//實(shí)例化web訪(fǎng)問(wèn)類(lèi)
  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. //提交請(qǐng)求
  21. Stream stream;
  22. stream = request.GetRequestStream();
  23. stream.Write(postdatabytes, 0, postdatabytes.Length);
  24. stream.Close();
  25. //接收響應(yīng)
  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出錯(cuò),;
  42. }
HttpWebRequest request = null;
            HttpWebResponse response = null;
            string gethost = string.Empty;
            CookieContainer cc = new CookieContainer();
            string Cookiesstr = string.Empty;
            try
            {
            //第一次POST請(qǐng)求
            string postdata = "email=" + UserName.Replace("@", "%40") + "&password=" + PassWord + "&origURL=" + HostUrl + "&domain=renren.com";//模擬請(qǐng)求數(shù)據(jù),,數(shù)據(jù)樣式可以用FireBug插件得到。人人網(wǎng)POST數(shù)據(jù)時(shí),,用戶(hù)名郵箱中的“@”變?yōu)椤?40”,,所以我們也要作此變化
            string  LoginUrl="http://www.renren.com/PLogin.do";
            request = (HttpWebRequest)WebRequest.Create(LoginUrl);//實(shí)例化web訪(fǎng)問(wèn)類(lèi)
            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;
            //提交請(qǐng)求
            Stream stream;
            stream = request.GetRequestStream();
            stream.Write(postdatabytes, 0, postdatabytes.Length);
            stream.Close();
            //接收響應(yīng)
            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出錯(cuò);
            }
            

注釋寫(xiě)的很詳細(xì)了,,在這就不再分析,,也許有人對(duì)request = (HttpWebRequest)WebRequest.Create(LoginUrl)有疑問(wèn),可以去google一下HttpWebRequest和WebRequest的區(qū)別,,簡(jiǎn)單來(lái)說(shuō)WebRequest是一個(gè)抽象類(lèi),,不能直接實(shí)例化,需要被繼承,,而HttpWebRequest繼承自WebRequest,。

再模擬第一個(gè)和第二個(gè)GET請(qǐng)求

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出錯(cuò)
  23. }
  24. try
  25. {
  26. //第二次GET請(qǐng)求
  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出錯(cuò)
  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出錯(cuò)
            }
            try
            {
            //第二次GET請(qǐng)求
            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出錯(cuò)
            }
            

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

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多