在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模拟登陆(附带验证码) 分类: C# .net2010-06-04 00:50 35647人阅读 评论(43) 收藏 举报 c#exceptionstreams ...

  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. leetcode easy problem set

     *勿以浮沙筑高台* 持续更新........     题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...

  2. .Net中的IO

    C#中的IO 本文环境为: Win 10 VS 2015 .net Framework 版本 4.0 File 类 File 类是一个工具类,可以用来判断文件是否存在和方便的创建文件, 读取.写入等操 ...

  3. [leetcode tree]104. Maximum Depth of Binary Tree

    求树的最大深度 class Solution(object): def maxDepth(self, root): if not root: return 0 left = self.maxDepth ...

  4. [leetcode sort]75. Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. bzoj4668: 冷战 并查集按秩合并

    题目链接 bzoj4668: 冷战 题解 按秩合并并查集,每次增长都是小集合倍数的两倍以上,层数不超过logn 查询路径最大值 LCT同解 代码 #include<bits/stdc++.h&g ...

  6. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  7. hash课堂测试补分博客

    题目要求: 开放地址法: 概念: 所谓的开放定址法就是一旦发生了冲突,就去寻找下一个空的散列地址,只要散列表足够大,空的散列地址总能找到,并将记录存入. 它的公式为: 解题过程(在下图中): 拉链法: ...

  8. Linux免密登录

    ssh连接上服务器 ssh -p 端口 用户名@ip地址 获取本地的pub ssh key cd ~/.ssh vi id_rsa.pub 拷贝里面的内容 将拷贝的内容放到服务器的authorized ...

  9. bzoj 2460 拟阵+判线性相关

    /************************************************************** Problem: 2460 User: idy002 Language: ...

  10. 更好的浏览器动画实现 requestAnimationFrame

    requestAnimationFrame 是专门为实现高性能的帧动画而设计的一个API: js一般是借助setTimeout或setInterval这两个函数实现动画,性能不佳. css3动画,性能 ...