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

2024-01-12 05:58

本文主要是介绍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. {
  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.下载验证码,保存在本地。

view plain copy to clipboard print ?
  1. /// <summary>
  2. /// 下载验证码图片并保存到本地
  3. /// </summary>
  4. /// <param name="Url">验证码URL</param>
  5. /// <param name="cookCon">Cookies值</param>
  6. /// <param name="savePath">保存位置/文件名</param>
  7. public staticbool 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数据

view plain copy to clipboard print ?
  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模拟登陆(附带验证码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/597015

相关文章

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出