博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

获取网页验证码

Posted on 2012-05-22 13:50  qianyz  阅读(3066)  评论(0编辑  收藏  举报

net有关得到验证码图片的措置(2011-08-11 02:03:27)

标签:

杂谈

 
  1、得到图片的三种体例

  ------------------------豆割符---------------------------------

  第一种体例

  HtmlElementCollection hc = webBrowser1.Document.getElementsByTagName_r("img");

  HtmlElement h0 = null;

  foreach (HtmlElement h in hc)

  {

  if (h.OuterHtml.Contains("code.en.img.php"))

  {

  h0 = h;

  MessageBox.Show(h.OuterHtml.ToString());

  }

  }

  MessageBox.Show(h0.GetAttribute("src"));

  string imgurl = h0.GetAttribute("src");

  pictureBox1.ImageLocation = imgurl;

  ------------------------豆割符---------------------------------

  第二种体例

  HtmlElement he = webBrowser1.Document.Images[10];

  mshtml.IHTMLImgElement img = (mshtml.IHTMLImgElement)(he.DomElement);

  string a = img.src;

  ------------------------豆割符---------------------------------

  第三种体例

  HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create("http://www.doudou.com/code.en.img.php");

  Bitmap bmp = new Bitmap(hwr.GetResponse().GetResponseStream());

  bmp.Save("L:\\test\\111.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

  2、没有ID的验证码图片,调用体例将图片生存到本地

  (偶然间我们需要得到网页上的图片,特殊是向验证码如许的图片.这个别例就是将网页上的图片得到到PictureBox中.)

  起首需要添加mshtml的援引,以后using mshtml;

  pictureBox1.Image = GetRegCodePic(webBrowser1, "", "code.en.img.php", ""); //第2-4个参数本身填

  //第2个参数为网页中图片的name(大概或ID也好用).

  //第3个参数为网页中图片的Src.

  //第4个参数为网页中图片的Alt.

  ////例<IMG height=80 alt="Registration Verification Code" src="......" width=290 border=0>

  pictureBox1.Image.Save("L:\\test\\111.bmp", pictureBox1.Image.RawFormat); //生存图片到本地

  public static Image GetRegCodePic(WebBrowser wbMail, string ImgName, string Src, string Alt)

  {

  HTMLDocument doc = (HTMLDocument)wbMail.Document.DomDocument;

  HTMLBody body = (HTMLBody)doc.body;

  IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();

  IHTMLControlElement Img;

  if (ImgName == "") //假定没有图片的名字,颠末过程Src或Alt中的关头字来取

  {

  int ImgNum = GetPicIndex(wbMail, Src, Alt);

  if (ImgNum == -1) return null;

  Img = (IHTMLControlElement)wbMail.Document.Images[ImgNum].DomElement;

  }

  else

  Img = (IHTMLControlElement)wbMail.Document.All[ImgName].DomElement;

  rang.add(Img);

  rang.execCommand("Copy", false, null);

  Image RegImg = Clipboard.GetImage();

  Clipboard.Clear();

  return RegImg;

  }

  public static int GetPicIndex(WebBrowser wbMail, string Src, string Alt)

  {

  int imgnum = -1;

  for (int i = 0; i < wbMail.Document.Images.Count; i++) //得到全部的Image元素

  {

  IHTMLImgElement img = (IHTMLImgElement)wbMail.Document.Images.DomElement;

  if (Alt == "")

  {

  if (img.src.Contains(Src)) return i;

  }

  else

  {

  if (!string.IsNullOrEmpty(img.alt))

  {

  if (img.alt.Contains(Alt)) return i;

  }

  }

  }

  return imgnum;

  }

  颠末过程调用GetRegCodePic便可以得到注册码图片了.下面是几个示例.

  示例1:

  下面是某个站的注册码图片的HTML部分源代码

  <IMG height=80 alt="Registration Verification Code" src="......" width=290 border=0>

  picturebox1.Image =GetRegCodePic(wbMail, "", "", "Registration Verification Code")

  示例2:

  下面是某个站的注册码图片的HTML部分源代码

  <IMG id=CAPTCHAImage src="......." name=CAPTCHAImage>

  picturebox1.Image =GetRegCodePic(wbMail, "CAPTCHAImage", "", "")

  

  


  

  


  3、C#利用mshtml的COM接口,将WebBrowser中的验证码图片读进内存

  在做发贴机,注水机,主动登录等环境时,假定碰到图片验证码的情况,就必要做验证码阐发了,这时间将验证码图片读进内存或生存在本地,就是必须的了

  固然,利用WebClient直接download验证码图片返来,是可以实现的,但是如许的话,实际上是请求了2次验证码,而直接从 webbrowser中,将验证码提取出来的话,便可以克制往一次网络通信,而窃冬也可以或许搪塞一些网站的每次随验证码动态返回的其他附属验证信息对主动登录变成的影响。

  .NET 自带的 WebBrowser 控件,利用起来可以说是相当方便了,微软已对其举办了比力美满的封装,但所谓“有得必有掉”,正是这类封装,也使得该控件的一些底层信息,全都看不到了。

  我们要实现的成果,就是利用了IE的DOM,这个在 WebBrowser 中,就力所不及了, 以是,我们这里要利用 mshtml 这个COM东西。

  

  [置顶].net有关得到验证码图片的措置

  

  先添加对 mshtml 的援引, 在 COM 页上, 选择 Microsoft HTML Object Library

  利用定名空间 using mshtml;

  核心代码:

  HTMLDocument html = (HTMLDocument)this.webBrowser1.Document.DomDocument;

  //下面代码中,得到图片的体例有很多,由于比力简略,我就不罗列了,直接用ID来做为例子的

  IHTMLControlElement img = (IHTMLControlElement)webBrowser1.Document.Images["MzImgExpPwd"].DomElement;

  IHTMLControlRange range = (IHTMLControlRange)((HTMLBody)html.body).createControlRange();

  range.add(img);

  range.execCommand("Copy", false, null);

  img = null;

  range = null;

  html = null;

  if (Clipboard.ContainsImage())

  {

  this.pictureBox1.Image = Clipboard.GetImage();

  }

  else

  {

  MessageBox.Show("推行不乐成");

  }

  Clipboard.Clear();

  4、webbrowser-C#-两种体例得到验证码

  //先添加对 mshtml 的援引, 在 COM 页上, 选择 Microsoft HTML Object Library

  //利用定名空间 using mshtml;

  //登录url 有id http://passport.csdn.net/UserLogin.aspx

  //登录url 没有id http://www.himporter.com/index.php?c=Members

  体例一 有id

  HTMLDocument html = (HTMLDocument)this.webBrowser1.Document.DomDocument;

  IHTMLControlElement img = (IHTMLControlElement)webBrowser1.Document.Images["MzImgExpPwd"].DomElement;

  IHTMLControlRange range = (IHTMLControlRange)((HTMLBody)html.body).createControlRange();

  range.add(img);

  range.execCommand("Copy", false, null);

  img = null;

  range = null;

  html = null;

  if

  (Clipboard.ContainsImage())

  {

  this.pictureBox1.Image = Clipboard.GetImage();

  }

  else

  {

  MessageBox.Show("推行不乐成");

  }

  Clipboard.Clear();

  体例二 没有id

  HtmlElementCollection hc = webBrowser1.Document.getElementsByTagName_r("img");

  HtmlElement h0 = null;

  foreach (HtmlElement h in hc)

  {

  if (h.OuterHtml.Contains("imgcode.html"))

  {

  h0 = h;

  //MessageBox.Show(h.OuterHtml.ToString());

  }

  }

  //MessageBox.Show( h0.GetAttribute("src"));

  string imgurl=h0.GetAttribute("src");

  pictureBox1.ImageLocation = imgurl;