吃个饭回来给你贴代码
const string FILE_NAME = "tmp.png";
private void button1_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.discuz.net/misc.php?mod=seccode&action=update&idhash=S0o0Fs40&inajax=1&ajaxtarget=seccode_S0o0Fs40");
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
HttpWebResponse respose = (HttpWebResponse)request.GetResponse();
Stream stream = respose.GetResponseStream();
string update = string.Empty;
using (StreamReader sr=new StreamReader(stream))
{
update=sr.ReadToEnd();
Regex re = new Regex("(?<update>update=\\d+)");
update = re.Match(update).Groups["update"].Value;
}
request = WebRequest.Create("http://www.discuz.net/misc.php?mod=seccode&update="+update+"&idhash=S0o0Fs40") as HttpWebRequest;
request.KeepAlive = true;
request.Referer = "http://www.discuz.net/member.php?mod=register";
//Referer是关键,没有这个不能正确取到验证码
respose = request.GetResponse() as HttpWebResponse;
stream = respose.GetResponseStream();
List<byte> bytes = new List<byte>();
while (true)
{
int bt = stream.ReadByte();
if (bt == -1)
break;
bytes.Add((byte)bt);
}
stream.Close();
stream.Dispose();
if (File.Exists(FILE_NAME))
{
if (pictureBox1.Image != null)
pictureBox1.Image.Dispose();
File.Delete(FILE_NAME);
Thread.Sleep(50);
}
File.WriteAllBytes(FILE_NAME, bytes.ToArray());
Thread.Sleep(50);
pictureBox1.Image = Image.FromFile(FILE_NAME);
pictureBox1.Height = pictureBox1.Image.Height;
pictureBox1.Width = pictureBox1.Image.Width;
}
@芬达: 识别部分多说无益,推荐个很好的图像处理库 AForge.NET ,识别过程大致是先处理图像,去噪、灰度、。。。。最终目的是二值化,最后是切割,将一个个的字切出来,二值化的值和特征库对比
@芬达: ok,尤其你这句话呵呵。多吃点哦,以后多多指点。再次感谢。
吃个饭回来给你贴代码
WebClient 应该可以解决你的问题
具体的呢?网上有个对百度的。但是对于discuz的好像不能
试下这中方式去取
static void Main(string[] args)
{
string html = null;
string url = "http://china.alibaba.com/keyword/promotion.htm?catId=14";
WebRequest req = WebRequest.Create(url);
WebResponse res = req.GetResponse();
Stream receiveStream = res.GetResponseStream();
Encoding encode = Encoding.GetEncoding("gb2312");
StreamReader sr = new StreamReader(receiveStream, encode);
char[] readbuffer = new char[256];
int n = sr.Read(readbuffer, 0, 256);
while (n > 0)
{
string str = new string(readbuffer, 0, n);
html += str;
n = sr.Read(readbuffer, 0, 256);
}
System.Console.Write(html);
}
将里面的某些东西改下就好了。
对discuz的 用不了,我不太了解discuz的验证码生成机制,抓包也抓不到,网上有httpwebrequest的和WebClient的例子但是都不是针对discuz论坛的。
@在路上01: 找到生成验证码的路径,再填写进去,就可以了,原理是一样的
你有没有设置Cookie啊
只是获取验证码,这个好像不用设置cookie吧。
@在路上01: 没有Cookie人家怎么信任你啊,你用Fiddler跟踪一下你程序发的请求和回应,与你在浏览器中发的请求和回应,具体有什么不同就知道了。