打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# 抓取网页验证码并post数据

如果想开发半自动的注册机程序,那么把验证码读取到winform里面,然后提交数据是必须的流程,这篇博文记录一下如何抓取网页上面的验证码,注意不是验证码识别。有的网站会验证Cookie,有的不会,本文包含Cookie读取提交。

首先生命一个全局的Cookie变量

  1. private CookieContainer cookie = new System.Net.CookieContainer();  


程序加载的时候读取验证码和Cookie

  1. public Form1()  
  2.         {  
  3.             InitializeComponent();  
  4.   
  5.             string checkcodeUrl = "验证码的url";  
  6.   
  7.             try  
  8.             {  
  9.                   
  10.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(checkcodeUrl);  
  11.   
  12.                 request.CookieContainer = new CookieContainer();   
  13.   
  14.                 Stream responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();  
  15.   
  16.                 cookie = request.CookieContainer;  
  17.   
  18.                 string cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);   
  19.   
  20.                 Image original = Image.FromStream(responseStream);  
  21.   
  22.                 Bitmap bitMap = new Bitmap(original);  
  23.   
  24.                 this.pictureBox1.Image = bitMap;  
  25.   
  26.                 responseStream.Close();  
  27.   
  28.             }  
  29.             catch (Exception exception)  
  30.             {  
  31.   
  32.                 MessageBox.Show("ERROR:" + exception.Message);  
  33.   
  34.             }  
  35.         }  


提交数据

  1. HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);  
  2.             webrequest.CookieContainer = cookie;  
  3.             HttpWebResponse response = (HttpWebResponse)webrequest.GetResponse();  
  4.             StreamReader read = new StreamReader(response.GetResponseStream(), Encoding.Default);  
  5.             string text = read.ReadToEnd();  


如果有看不懂的欢迎留言咨询,只写了一部分必须的代码。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Python网络爬虫学习实战:爬虫快速入门
R语言爬虫系列6|动态数据抓取范例
Linux下cURL使用教程之二:HTTP协议概述
史上最详细的抓包教程 看到就是赚到
PHP 中使用CURL实现GET和POST请求
使用InternetSetCookie 删除时一定要传入path参数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服