c# 使用 HttpWebRequest模拟登陆

c# 使用 HttpWebRequest模拟登陆(附带验证码)

分类: C# .net

在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等。

先说下流程

1.使用httpwebrequest先进入你要登录的网站,获取cookie

2.使用第一步获取的cookie到验证码的网页将验证码下载下来。

3.使用Post数据 发送至网站。如果有cookie则继续保存。

4.使用第三步的cookie登陆相关网页操作。

 

获取相关数据可以使用抓包工具进行抓取,如httpwatch。(网上下载的好多都有病毒,下载的时候注意点)

1。

 

[c-sharp]  view plain copy
 
  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. {  
  9.     HttpWebRequest request;  
  10.     HttpWebResponse response;  
  11.     ArrayList list = new ArrayList();  
  12.     request = WebRequest.Create(postUrl) as HttpWebRequest;  
  13.     request.Method = "GET";  
  14.     request.UserAgent = "Mozilla/4.0";  
  15.     request.CookieContainer = cookie;  
  16.     request.KeepAlive = true;  
  17.   
  18.     request.CookieContainer = cookie;  
  19.     try  
  20.     {  
  21.         //获取服务器返回的资源  
  22.         using (response = (HttpWebResponse)request.GetResponse())  
  23.         {  
  24.             using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))  
  25.             {  
  26.                 cookie.Add(response.Cookies);  
  27.                 //保存Cookies  
  28.                 list.Add(cookie);  
  29.                 list.Add(reader.ReadToEnd());  
  30.                 list.Add(Guid.NewGuid().ToString());//图片名  
  31.             }  
  32.         }  
  33.     }  
  34.     catch (WebException ex)  
  35.     {  
  36.         list.Clear();  
  37.         list.Add("发生异常/n/r");  
  38.         WebResponse wr = ex.Response;  
  39.         using (Stream st = wr.GetResponseStream())  
  40.         {  
  41.             using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))  
  42.             {   
  43.                 list.Add(sr.ReadToEnd());  
  44.             }  
  45.         }  
  46.     }  
  47.     catch (Exception ex)  
  48.     {  
  49.         list.Clear();  
  50.         list.Add("5");  
  51.         list.Add("发生异常:" + ex.Message);  
  52.     }  
  53.     return list;  
  54. }  

 

2.下载验证码,保存在本地。

 

[c-sharp]  view plain copy
 
  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数据

 

[c-sharp]  view plain copy
 
  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. {  
  12.     ArrayList list = new ArrayList();  
  13.     HttpWebRequest request;  
  14.     HttpWebResponse response;  
  15.     ASCIIEncoding encoding = new ASCIIEncoding();  
  16.     request = WebRequest.Create(postUrl) as HttpWebRequest;  
  17.     byte[] b = encoding.GetBytes(postData);  
  18.     request.UserAgent = "Mozilla/4.0";  
  19.     request.Method = "POST";  
  20.     request.CookieContainer = cookie;  
  21.     request.ContentLength = b.Length;  
  22.     using (Stream stream = request.GetRequestStream())  
  23.     {  
  24.         stream.Write(b, 0, b.Length);  
  25.     }  
  26.   
  27.     try  
  28.     {  
  29.         //获取服务器返回的资源  
  30.         using (response = request.GetResponse() as HttpWebResponse)  
  31.         {  
  32.             using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))  
  33.             {  
  34.                 if (response.Cookies.Count > 0)  
  35.                     cookie.Add(response.Cookies);  
  36.                 list.Add(cookie);  
  37.                 list.Add(reader.ReadToEnd());  
  38.             }  
  39.         }  
  40.     }  
  41.     catch (WebException wex)  
  42.     {  
  43.         WebResponse wr = wex.Response;  
  44.         using (Stream st = wr.GetResponseStream())  
  45.         {  
  46.             using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))  
  47.             {  
  48.                 list.Add(sr.ReadToEnd());  
  49.             }  
  50.         }  
  51.     }  
  52.     catch (Exception ex)  
  53.     {  
  54.         list.Add("发生异常/n/r"+ex.Message);  
  55.     }  
  56.     return list;  
  57. }  

 

4。就是第三步请求的链接地址换一个就行了

 

好了

以上核心代码已经贴出了

具体实现需要靠你们按照你们自己的逻辑

 

还有一些header能不写就不写,因为我2天前一直在获取返回response这地方报500错误。

找了N多代码,看了N多资料都不可以。最后将一些header注释掉就可以了,真郁闷。

 

转载于:https://www.cnblogs.com/alex-13/p/4848864.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值