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

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

人人網登錄請求

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

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";//模擬請求數據,數據樣式可以用FireBug插件得到。人人網POST數據時,用戶名郵箱中的“@”變爲“%40”,所以我們也要作此變化   
    string  LoginUrl="http://www.renren.com/PLogin.do";   
      request = (HttpWebRequest)WebRequest.Create(LoginUrl);//實例化web訪問類   
    request.Method = "POST";//數據提交方式爲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跳轉地址   
    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的區別,簡單來說WebRequest是一個抽象類,不能直接實例化,需要被繼承,而HttpWebRequest繼承自WebRequest。

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

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();   
    //設置cookie   
    Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);   
    //取再次跳轉鏈接   
    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();   
    //設置cookie   
    Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);   
    request.Abort();   
    response.Close();   
}   
catch (Exception)   
{   
    //第二次GET出錯   
} 

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

本篇文章轉載自:http://www.cnblogs.com/zhwl/archive/2012/03/14/2396090.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章