分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报

在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模拟登陆的更多相关文章

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

    在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等. 先说下流程 1.使用httpwebrequest先进入你要登录的 ...

  2. 使用C#的HttpWebRequest模拟登陆访问人人网

    使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路: 第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交的 ...

  3. 使用C#的HttpWebRequest模拟登陆访问人人网(转)

    无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交 ...

  4. 使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)

    前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏 ...

  5. 转:使用C#的HttpWebRequest模拟登陆网站

    这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据. ...

  6. 使用C#的HttpWebRequest模拟登陆网站

    很久没有写新的东西了,今天在工作中遇到的一个问题,感觉很有用,有种想记下来的冲动. 这篇文章是有关模拟登录网站方面的. 实现步骤: 启用一个web会话 发送模拟数据请求(POST或者GET) 获取会话 ...

  7. HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用

    [一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3284481.html] PostLogin :登录,并保存Cookie 1 public st ...

  8. C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用

    public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cooki ...

  9. 通过HttpWebRequest实现模拟登陆

    1>通过HttpWebRequest模拟登陆 using System; using System.Collections.Generic; using System.Linq; using S ...

随机推荐

  1. Java核心技术 卷Ⅰ 基础知识(2)

    第四章 对象与类 基于类的访问权限 静态域 类的设计技巧

  2. smoke.js是一款基于HTML5 Canvas的逼真烟雾特效js插件。通过该js插件,可以非常轻松的在页面中制作出各种烟雾效果。

    Smoke.js 是一个浏览器默认警告系统的JavaScript替代品,如果你想要跨浏览器与平台的标准化JavaScript警告窗口,Smoke.js就是你想要的. Smoke.js是一个轻量级且灵活 ...

  3. 字典(Tire树)

    4189 字典  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个 ...

  4. PSR

    目前包括以下几个规范: PSR-0(弃用) PSR-1 PSR-2 PSR-3 PSR-4 1.PSR-0 自动加载规范,此规范已被启用-本规范已于2014年10月21日被标记为弃用,目前新的替代规范 ...

  5. Imagine Cup 微软“创新杯”全球学生科技大赛

    一. 介绍 微软创新杯微博:http://blog.sina.com.cn/u/1733906825 官方站点:https://www.microsoft.com/china/msdn/student ...

  6. 【CodeVS】1293

    输入输出样例 思路:看到题目我萌第一眼想到的肯定是求联通快对吧,但是这个联通快有点奇特,因为 这样他也算是一个联通快.解决此题其实有三种解法:1)宽搜(这个符合基本法):2)并查集:3)灌水法 但是蒟 ...

  7. jsp页面根据当前时间和定义时间差计算动态倒计时

    jsp页面根据当前时间和定义时间差计算动态倒计时http://www.jb51.net/article/74140.htm    var maxtime =1000*60; //半个小时,按秒计算,自 ...

  8. CodeForces 667C Reberland Linguistics

    $dp$. 题意中有一个词组:$in$ $a$ $row$,是连续的意思.... 因此这题只要倒着$dp$一下就可以了.$f[i][0]$表示从$i$位置往后割两个能否割,$f[i][1]$表示从$i ...

  9. python向服务器发送邮件事例

    import osimport sysimport re __author__ = 'xiaoming' import requestststr = '<div>\n<ul>\ ...

  10. 杂谈3之English

    1.面向过程(OPP):Orient Procedure Program (C语言) 2.面向对象(OOP):Orient ObjectProgram(Java) 3.面向对象的三大特征:继承Inhe ...