c# 使用 HttpWebRequest模擬登陸(附帶驗證碼)

在C#中,可以使用HttpWebRequest進行相關的模擬登陸,登陸後進行相關的操作,比如抓取數據,頁面分析,製作相關登陸助手等等。

先說下流程

1.使用httpwebrequest先進入你要登錄的網站,獲取cookie

2.使用第一步獲取的cookie到驗證碼的網頁將驗證碼下載下來。

3.使用Post數據 發送至網站。如果有cookie則繼續保存。

4.使用第三步的cookie登陸相關網頁操作。

獲取相關數據可以使用抓包工具進行抓取,如httpwatch。(網上下載的好多都有病毒,下載的時候注意點)

1。

  1. /// <summary> 
  2. /// 通過get方式請求頁面,傳遞一個實例化的cookieContainer 
  3. /// </summary> 
  4. /// <param name="postUrl"></param> 
  5. /// <param name="cookie"></param> 
  6. /// <returns></returns> 
  7. public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie) 
  8.     HttpWebRequest request; 
  9.     HttpWebResponse response; 
  10.     ArrayList list = new ArrayList(); 
  11.     request = WebRequest.Create(postUrl) as HttpWebRequest; 
  12.     request.Method = "GET"
  13.     request.UserAgent = "Mozilla/4.0"
  14.     request.CookieContainer = cookie; 
  15.     request.KeepAlive = true
  16.  
  17.     request.CookieContainer = cookie; 
  18.     try 
  19.     { 
  20.         //獲取服務器返回的資源 
  21.         using (response = (HttpWebResponse)request.GetResponse()) 
  22.         { 
  23.             using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default)) 
  24.             { 
  25.                 cookie.Add(response.Cookies); 
  26.                 //保存Cookies 
  27.                 list.Add(cookie); 
  28.                 list.Add(reader.ReadToEnd()); 
  29.                 list.Add(Guid.NewGuid().ToString());//圖片名 
  30.             } 
  31.         } 
  32.     } 
  33.     catch (WebException ex) 
  34.     { 
  35.         list.Clear(); 
  36.         list.Add("發生異常/n/r"); 
  37.         WebResponse wr = ex.Response; 
  38.         using (Stream st = wr.GetResponseStream()) 
  39.         { 
  40.             using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default)) 
  41.             {  
  42.                 list.Add(sr.ReadToEnd()); 
  43.             } 
  44.         } 
  45.     } 
  46.     catch (Exception ex) 
  47.     { 
  48.         list.Clear(); 
  49.         list.Add("5"); 
  50.         list.Add("發生異常:" + ex.Message); 
  51.     } 
  52.     return list; 

2.下載驗證碼,保存在本地。

  1. /// <summary> 
  2.         /// 下載驗證碼圖片並保存到本地 
  3.         /// </summary> 
  4.         /// <param name="Url">驗證碼URL</param> 
  5.         /// <param name="cookCon">Cookies值</param> 
  6.         /// <param name="savePath">保存位置/文件名</param> 
  7.         public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath) 
  8.         { 
  9.             bool bol = true
  10.             HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url); 
  11.             //屬性配置 
  12.             webRequest.AllowWriteStreamBuffering = true
  13.             webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials; 
  14.             webRequest.MaximumResponseHeadersLength = -1; 
  15.             webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
  16.             webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)"
  17.             webRequest.ContentType = "application/x-www-form-urlencoded"
  18.             webRequest.Method = "GET"
  19.             webRequest.Headers.Add("Accept-Language", "zh-cn"); 
  20.             webRequest.Headers.Add("Accept-Encoding", "gzip,deflate"); 
  21.             webRequest.KeepAlive = true
  22.             webRequest.CookieContainer = cookCon; 
  23.             try 
  24.             { 
  25.                 //獲取服務器返回的資源 
  26.                 using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
  27.                 { 
  28.                     using (Stream sream = webResponse.GetResponseStream()) 
  29.                     { 
  30.                         List<byte> list = new List<byte>(); 
  31.                         while (true
  32.                         { 
  33.                             int data = sream.ReadByte(); 
  34.                             if (data == -1) 
  35.                                 break
  36.                             list.Add((byte)data); 
  37.                         } 
  38.                         File.WriteAllBytes(savePath, list.ToArray()); 
  39.                     } 
  40.                 } 
  41.             } 
  42.             catch (WebException ex) 
  43.             { 
  44.                 bol = false
  45.             } 
  46.             catch (Exception ex) 
  47.             { 
  48.                 bol = false
  49.             } 
  50.             return bol; 
  51.         } 

3。發送post數據

  1. /// <summary> 
  2. /// 發送相關數據至頁面 
  3. /// 進行登錄操作 
  4. /// 並保存cookie 
  5. /// </summary> 
  6. /// <param name="postData"></param> 
  7. /// <param name="postUrl"></param> 
  8. /// <param name="cookie"></param> 
  9. /// <returns></returns> 
  10. public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie) 
  11.     ArrayList list = new ArrayList(); 
  12.     HttpWebRequest request; 
  13.     HttpWebResponse response; 
  14.     ASCIIEncoding encoding = new ASCIIEncoding(); 
  15.     request = WebRequest.Create(postUrl) as HttpWebRequest; 
  16.     byte[] b = encoding.GetBytes(postData); 
  17.     request.UserAgent = "Mozilla/4.0"
  18.     request.Method = "POST"
  19.     request.CookieContainer = cookie; 
  20.     request.ContentLength = b.Length; 
  21.     using (Stream stream = request.GetRequestStream()) 
  22.     { 
  23.         stream.Write(b, 0, b.Length); 
  24.     } 
  25.  
  26.     try 
  27.     { 
  28.         //獲取服務器返回的資源 
  29.         using (response = request.GetResponse() as HttpWebResponse) 
  30.         { 
  31.             using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 
  32.             { 
  33.                 if (response.Cookies.Count > 0) 
  34.                     cookie.Add(response.Cookies); 
  35.                 list.Add(cookie); 
  36.                 list.Add(reader.ReadToEnd()); 
  37.             } 
  38.         } 
  39.     } 
  40.     catch (WebException wex) 
  41.     { 
  42.         WebResponse wr = wex.Response; 
  43.         using (Stream st = wr.GetResponseStream()) 
  44.         { 
  45.             using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default)) 
  46.             { 
  47.                 list.Add(sr.ReadToEnd()); 
  48.             } 
  49.         } 
  50.     } 
  51.     catch (Exception ex) 
  52.     { 
  53.         list.Add("發生異常/n/r"+ex.Message); 
  54.     } 
  55.     return list; 

4。就是第三步請求的鏈接地址換一個就行了

好了

以上核心代碼已經貼出了

具體實現需要靠你們按照你們自己的邏輯

還有一些header能不寫就不寫,因爲我2天前一直在獲取返回response這地方報500錯誤。

找了N多代碼,看了N多資料都不可以。最後將一些header註釋掉就可以了,真鬱悶。

 

 

http://blog.csdn.net/vip__888/article/details/5646260

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