在博客园里 看到关于 C#登录网站识别验证码的文章
在winform添加 pictureBox显示网站上生成的验证码图片!网站验证码要是asp /asdp.net生成的验证码图片,都能在pictureBox显示出来!可PHP脚本生成的验证码,程序就出问题了,
以下是代码
private void Form1_Load(object sender, EventArgs e)
{ }
string aspcookie = "";
private string html = "";
private void button3_Click(object sender, EventArgs e)
{ html = Http.GetHtml("http://www.52sunny.net", out aspcookie);
aspcookie = aspcookie.Split(';')[0];
richTextBox1.AppendText("获得的Cookie:" + aspcookie + "\r\n");
string header = "";
byte[] b = {};
Image img = new Bitmap(
Http.GetStreamByBytes("http://www.52sunny.net", "http://www.52sunny.net/include/class/img.php", b,
aspcookie, out header));//获得验证码图片
this.pictureBox1.Image = img;
} }
}以下是HTTP.CS类 public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie,out string header)
{
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(server), cookie); httpWebRequest.CookieContainer = co; httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.Referer = "http://www.52sunny.net";
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "Post";
httpWebRequest.ContentLength = byteRequest.Length;
Stream stream;
stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
contentLength = webResponse.ContentLength;
byte[] outBytes = new byte[contentLength]; //VS在这里提示 “算术运算导致溢出”。 排错“确保不会被清零”
outBytes = ReadFully(getStream);
getStream.Close();
return outBytes;
}
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[128];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}ASP/ ASP.NET 生成的验证码 程序正常运行,PHP的不行! 头都大了
望各位高手解决,谢谢!
在winform添加 pictureBox显示网站上生成的验证码图片!网站验证码要是asp /asdp.net生成的验证码图片,都能在pictureBox显示出来!可PHP脚本生成的验证码,程序就出问题了,
以下是代码
private void Form1_Load(object sender, EventArgs e)
{ }
string aspcookie = "";
private string html = "";
private void button3_Click(object sender, EventArgs e)
{ html = Http.GetHtml("http://www.52sunny.net", out aspcookie);
aspcookie = aspcookie.Split(';')[0];
richTextBox1.AppendText("获得的Cookie:" + aspcookie + "\r\n");
string header = "";
byte[] b = {};
Image img = new Bitmap(
Http.GetStreamByBytes("http://www.52sunny.net", "http://www.52sunny.net/include/class/img.php", b,
aspcookie, out header));//获得验证码图片
this.pictureBox1.Image = img;
} }
}以下是HTTP.CS类 public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie,out string header)
{
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(server), cookie); httpWebRequest.CookieContainer = co; httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept =
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.Referer = "http://www.52sunny.net";
httpWebRequest.UserAgent =
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
httpWebRequest.Method = "Post";
httpWebRequest.ContentLength = byteRequest.Length;
Stream stream;
stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
header = webResponse.Headers.ToString();
getStream = webResponse.GetResponseStream();
contentLength = webResponse.ContentLength;
byte[] outBytes = new byte[contentLength]; //VS在这里提示 “算术运算导致溢出”。 排错“确保不会被清零”
outBytes = ReadFully(getStream);
getStream.Close();
return outBytes;
}
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[128];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}ASP/ ASP.NET 生成的验证码 程序正常运行,PHP的不行! 头都大了
望各位高手解决,谢谢!
你可以点击http://kf.hdfybjy.com/zxyy/的"预约"看看.
验证码能够读出.
我估计是不是你的php除了发验证码还发了一些垃圾,你跟踪
contentLength = webResponse.ContentLength;
这一步看看contentLength 值是多少?和你的验证码的图片大小是否一致.