六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 717|回复: 0

c# 使用 HttpWebRequest模拟登陆(附带验证码) -it技术论坛

[复制链接]
 楼主| 发表于 2014-2-18 01:02:05 | 显示全部楼层 |阅读模式
c# 使用 HttpWebRequest模拟登陆(附带验证码) -it技术论坛-it论坛-计算机论坛
在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.         {
  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.             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;
  53.         }
复制代码
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.         {
  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.             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;
  56.         }
复制代码
4。就是第三步请求的链接地址换一个就行了

好了
以上核心代码已经贴出了
具体实现需要靠你们按照你们自己的逻辑

还有一些header能不写就不写,因为我2天前一直在获取返回response这地方报500错误。
找了N多代码,看了N多资料都不可以。最后将一些header注释掉就可以了,真郁闷。



c# 使用 HttpWebRequest模拟登陆(附带验证码) -it技术论坛-it论坛-计算机论坛
本文摘自:http://blog.csdn.net/vip__888/article/details/5646260

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表