十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
一般登陆网站时候首先要打开一个网页对吧?
创新互联是一家专注于网站设计、网站制作与策划设计,张家口网站建设哪家好?创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:张家口等地区。张家口做网站价格咨询:18982081108
那首先要 GET 一个网址。GetResponse后,得到的流就是这个页面的源码。
源码里肯定会包含这个验证码的提问段(可能是个图片的网址,也可能是个 5+5=? 之类的字符串之类的),可以分析一下这段代码出现的位置,让程序自动寻找。找到这个图片的网址,把这个图片 GET 下来,然后,就是orc识别或你人工识别咯。。。
Dim CtrlRange As Object
On Error Resume Next
Do
Err.Clear()
CtrlRange = Ie.Document.Body.DomElement.createControlRange()
CtrlRange.Add(Ie.Document.DomDocument.GetElementById("verifypic"))
CtrlRange.execCommand("Copy") '获取验证码
If Clipboard.ContainsData(DataFormats.Bitmap) Then
PictureBox1.Image = Clipboard.GetData(DataFormats.Bitmap) '在Picture1控件上显示
End If
If Err.Number 0 Then WaitingMS(100) ‘延时
Loop While Err.Number 0
以前找的 vbnet里也可以用
这个是可以使用api来做的。图像处理之类的函数。
另外,还有一种方法是将所有的字符和数字做成不同的小图片进行拼接而成的一个字符串序列;
小图片要自己去做;效果就可以任意展现了。
我会C#.net的,不过逻辑应该一样,给你看下吧第一步: 创建生成随机字符的方法private string CreateValidate(int count)
{
//定义验证码中所有的字符
string allchar = "1,2,3,4,5,6,7,8,9,0,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
//将验证码中所有的字符保存在一个字符串数组中
string[] allchararray = allchar.Split(',');
//初始化一个随机数
string randomcode = "";
int temp = -1;
//生成一个随机对象
Random rand = new Random();
//根据验证码的位数循环
for (int i = 0; i count; i++)
{
//主要是防止生成相同的验证码
if (temp != -1)
{
//加入时间的刻度
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
//相等的话重新生成
return CreateValidate(count);
}
temp = t;
randomcode += allchararray[t];
}
//在Session中保存随机验证码
Session["Valid"] = randomcode;
//返回生成的随机字符
return randomcode;
}第二步:创建生成图片的方法:private void CreateImageM(string validateCode)
{
//图像的宽度-与验证码的长度成一定比例
int iwidth = (int)(validateCode.Length * 11.5);
//创建一个长20,宽iwidth的图像对象
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
//创建一个新绘图对象
Graphics g = Graphics.FromImage(image);
//绘图用的字体和字号
Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
//绘图用的刷子大小
Brush b = new System.Drawing.SolidBrush(Color.White);
//清除背景色,并以深橄榄绿的颜色填充
g.Clear(Color.DarkOliveGreen);
//格式化刷子属性-用指定的刷子、颜色等在指定的范围内画图
g.DrawString(validateCode, f, b, 3, 3);
//创建铅笔对象
Pen blackPen = new Pen(Color.Black, 0);
//创建随机对象
Random rand = new Random();
//随机画线
for (int i = 0; i 5; i++)
{
int y = rand.Next(image.Height);
g.DrawLine(blackPen, 0, y, image.Width, y);
}
//输出绘图
System.IO.MemoryStream ms = new System.IO.MemoryStream();
//将图像保存到指定的流
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
//配置输出类型
Response.ContentType = "image/Jpeg";
//输出内容
Response.BinaryWrite(ms.ToArray());
//清空不需要的资源
g.Dispose();
image.Dispose();
}第三步:在page_Load事件中调用CreateImageM(CreateValidate(4));第四步:判断服务器端的验证码是否与用户输入的相同protected void Button1_Click(object sender, EventArgs e)
{
//判断服务器端的验证码是否与用户输入的相同
if (TextBox1.Text == Session["Valid"].ToString())
{
//相同则提示欢迎信息
Response.Write("script language='javascript'alert('欢迎光临')/script");
}
else
{
//不相同则提示错误信息
Response.Write("script language='javascript'alert('对不起,验证码错误')/script");
}
}
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Drawing
Partial Public Class VerifyCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Session("VerifyCode") = MakeValidateCode()
CreateImage(Session("VerifyCode").ToString())
End Sub
Private Sub CreateImage(ByVal checkCode As String)
Dim iwidth As Integer = CInt(checkCode.Length * 12) + 10
Dim image As New System.Drawing.Bitmap(iwidth, 25)
Dim g As Graphics = Graphics.FromImage(image)
g.Clear(Color.White)
'定义颜色
Dim c() As Color = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }
'定义字体
Dim font() As String = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }
Dim rand As New Random()
'随机输出噪点
For i As Integer = 0 To 4
Dim x As Integer = rand.Next(image.Width)
Dim y As Integer = rand.Next(image.Height)
g.DrawRectangle(New Pen(Color.LightGray, 0), x, y, 1, 1)
Next i
'输出不同字体和颜色的验证码字符
For i As Integer = 0 To checkCode.Length - 1
Dim cindex As Integer = rand.Next(7)
Dim findex As Integer = rand.Next(5)
Dim f As Font = New System.Drawing.Font("宋体", 12, System.Drawing.FontStyle.Regular)
Dim b As Brush = New System.Drawing.SolidBrush(c(cindex))
Dim ii As Integer = 4
If (i + 1) Mod 2 = 0 Then
ii = 2
End If
g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii)
Next i
'画一个边框
g.DrawRectangle(New Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1)
'输出到浏览器
Dim ms As New System.IO.MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Response.ClearContent()
Response.ContentType = "image/Jpeg"
Response.BinaryWrite(ms.ToArray())
g.Dispose()
image.Dispose()
End Sub
Private Function MakeValidateCode() As String
'0 i o
Dim s() As Char = { "0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c, "G"c, "J"c, "K"c, "L"c, "M"c, "N"c, "P"c, "Q"c, "R"c, "S"c, "T"c, "U"c, "V"c, "W"c, "X"c, "Y"c, "Z"c }
Dim num As String = ""
Dim r As New Random()
For i As Integer = 0 To 3
num = s(r.Next(0, s.Length - 1)).ToString()
Next i
Return num
End Function
End Class
建一个YanzhenCard.aspx文件
保留%@ Page Language="VB" AutoEventWireup="false" CodeFile="YanzhenCard.aspx.vb" Inherits="YanzhenCard" %,其余删除
如下是代码:
Imports System.IO
Imports System.Drawing
Partial Class YanzhenCard
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim VNum As String = GenerateRandom(4)
Session("VNum") = VNum
ValidateCode(VNum)
End If
End Sub
Private Sub ValidateCode(ByVal VNum As String)
Dim Gheight As Integer = CType((VNum.Length * 15), Integer)
Dim Img As Bitmap = New Bitmap(Gheight, 20) 'Bitmap是用于处理由像素数据定义的图像
Dim g As Graphics = Graphics.FromImage(Img) 'Graphics.FromImage 从指定的Image创建新的Graphics
g.DrawString(VNum, New Font("Arial", 12), New SolidBrush(Color.Red), 5, 0) 'g.DrawString()在指定位置并且用指定的Brush和Font对象绘制指定的文本字符串。
Dim ms As MemoryStream = New MemoryStream '创建其支持存储区为内存的流。
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/PNG"
Response.BinaryWrite(ms.ToArray)
Response.Write(VNum)
g.Dispose()
Img.Dispose()
Response.End()
End Sub
Private Shared constant As Char() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Public Shared Function GenerateRandom(ByVal Length As Integer) As String
Dim newRandom As System.Text.StringBuilder = New System.Text.StringBuilder(26)
Dim rd As Random = New Random
Dim i As Integer = 0
While i Length
newRandom.Append(constant(rd.Next(26)))
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return newRandom.ToString
End Function
End Class
调用方法:asp:ImageButton ID="Yanzhengma_Card" runat="server" ImageUrl="YanzhenCard.aspx" Height="20px" Width="75px" BorderColor="#6699cc" BorderStyle="Solid" BorderWidth="1px" /