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

相关文章

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

基于C#实现MQTT通信实战

《基于C#实现MQTT通信实战》MQTT消息队列遥测传输,在物联网领域应用的很广泛,它是基于Publish/Subscribe模式,具有简单易用,支持QoS,传输效率高的特点,下面我们就来看看C#实现... 目录1、连接主机2、订阅消息3、发布消息MQTT(Message Queueing Telemetr

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

Python logging模块使用示例详解

《Pythonlogging模块使用示例详解》Python的logging模块是一个灵活且强大的日志记录工具,广泛应用于应用程序的调试、运行监控和问题排查,下面给大家介绍Pythonlogging模... 目录一、为什么使用 logging 模块?二、核心组件三、日志级别四、基本使用步骤五、快速配置(bas

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

C#特性(Attributes)和反射(Reflection)详解

《C#特性(Attributes)和反射(Reflection)详解》:本文主要介绍C#特性(Attributes)和反射(Reflection),具有很好的参考价值,希望对大家有所帮助,如有错误... 目录特性特性的定义概念目的反射定义概念目的反射的主要功能包括使用反射的基本步骤特性和反射的关系总结特性

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到