我想将它集成到Windows应用程序。我使用Visual Studio 2010中,它是一个C#Windows窗体应用程序。
解决方案 公共静态图像DownloadReCaptcha(字符串键,裁判串挑战)
{
尝试
{
Web客户端的客户端=新的Web客户端();
字符串响应= client.DownloadString(的String.Format(http://api.recaptcha.net/challenge?k={0}键));
比赛比赛= Regex.Match(回应,挑战:'?(+)');
如果(match.Captures.Count == 0)
{
挑战= NULL;
返回null;
}
挑战= match.Groups [1] .value的;
如果(File.Exists(captcha.jpg))File.Delete(captcha.jpg);
client.DownloadFile(的String.Format(http://www.google.com/recaptcha/api/image?c={0},挑战),
captcha.jpg);
返回Image.FromFile(captcha.jpg);
}
赶上(例外)
{
挑战= NULL;
返回null;
}
}
使用它像:
字符串的挑战= NULL; //你需要这个提交验证码答案
pictureBox1.Image = DownloadReCaptcha(验证码网站键,裁判的挑战);
在网页的HTML源代码,你会发现像
&LT;脚本类型=文/ JavaScript的 src="http://www.google.com/recaptcha/api/challenge?k=6LexLsMSAAAAABUuI6bvUYfxaumgcu0vGiEFotDA"></script>
键= 6LexLsMSAAAAABUuI6bvUYfxaumgcu0vGiEFotDA
I'd like to integrate it into a Windows application. I'm using Visual Studio 2010 and it is a C# Windows Forms application.
解决方案 public static Image DownloadReCaptcha(string key, ref string challenge)
{
try
{
WebClient client = new WebClient();
string response = client.DownloadString(string.Format("http://api.recaptcha.net/challenge?k={0}", key));
Match match = Regex.Match(response, "challenge : '(.+?)'");
if (match.Captures.Count == 0)
{
challenge = null;
return null;
}
challenge = match.Groups[1].Value;
if (File.Exists("captcha.jpg")) File.Delete("captcha.jpg");
client.DownloadFile(string.Format("http://www.google.com/recaptcha/api/image?c={0}", challenge),
"captcha.jpg");
return Image.FromFile("captcha.jpg");
}
catch (Exception)
{
challenge = null;
return null;
}
}
Use it like :
string challenge = null; // you will need this to submit captcha answer
pictureBox1.Image = DownloadReCaptcha("reCaptcha site key", ref challenge);
In the HTML source of web-page you will find something like
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LexLsMSAAAAABUuI6bvUYfxaumgcu0vGiEFotDA"></script>
key = 6LexLsMSAAAAABUuI6bvUYfxaumgcu0vGiEFotDA