一、生成验证码图片并显示代码
由于C#代码难度不大,这里就不再细讲了,仅贴出代码,最后给出源码,大家可以参考;-
private void button1_Click(object sender, EventArgs e)
-
{
-
System.Net.ServicePointManager.Expect100Continue = false;
-
Uri uri = new Uri("http://edu.sqzycc.com/inc/checkcode.asp");
-
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
-
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-
Stream resStream = response.GetResponseStream();//得到验证码数据流
-
Bitmap sourcebm = new Bitmap(resStream);//初始化Bitmap图片
-
//sourcebm.Save(@"C: est.gif", ImageFormat.Gif);//可以保存到本地
-
this.pictureBox1.Image = sourcebm;
-
}
二、向服务器处理程序POST数据
-
private void button2_Click(object sender, EventArgs e)
-
{
-
System.Net.ServicePointManager.Expect100Continue = false;
-
//string strsubmit = "YES";
-
ASCIIEncoding encoding = new ASCIIEncoding();
-
string data2 = this.textBox1.Text;
-
string postData = "pr_id=1&hxr=26&hxr=30&hxr=33&hxr=39&hxr=53&hxr=54&hxr=66&hxr=69&hxr=70&hxr=81&code=" + data2 + "&cid=411402198807885982&x=9&y=5";
-
this.label1.Text = postData;
-
byte[] data = encoding.GetBytes(postData);
-
// Prepare web request...
-
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://edu.sqzycc.com/do.asp?action=post");
-
myRequest.Method = "POST";
-
myRequest.ContentType = "application/x-www-form-urlencoded";
-
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
-
myRequest.ContentLength = data.Length;
-
Stream newStream = null;
-
try
-
{
-
newStream = myRequest.GetRequestStream();
-
}
-
catch (Exception ex)
-
{
-
this.label2.Text = ex.Message;
-
return;
-
}
-
// Send the data.
-
newStream.Write(data, 0, data.Length);
-
newStream.Close();
-
// Get response
-
HttpWebResponse httpWebResponse = null;
-
try
-
{
-
httpWebResponse = (HttpWebResponse)myRequest.GetResponse();
-
}
-
catch (Exception ex)
-
{
-
this.label2.Text = ex.Message;
-
return;
-
}
-
Stream responseStream = httpWebResponse.GetResponseStream();
-
//这段用来获取返回信息的编码方式,以防乱码
-
Encoding MyEncoding = Encoding.Default;
-
// 如果要下载的页面经过压缩,则先解压
-
if (httpWebResponse.ContentEncoding.ToLower().IndexOf("gzip") >= 0)
-
{
-
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
-
}
-
if (MyEncoding == null)
-
{
-
MyEncoding = Encoding.Default;
-
}
-
StreamReader reader = new StreamReader(responseStream, MyEncoding);
-
string content = reader.ReadToEnd();
-
this.label2.Text = content;
-
}
三、关于验证码自动识别
关于验证码自动识别的问题,昨天我稍微研究了一下,主要用到Tesseract-OCR,这个东东就可以做到图片自动识别,但我试了下,一般而言,识别率不太高;如果针对特定网站做验证识别的话,可能要对Tesseract进行数据训练,以提高准确率。版权申明:本篇文章属于原创,转载请注明出自微信投票网。原文地址: http://www.aivote.com/12981.html