我可以在Windows应用程序中而不是在网页中包含reCAPTCHA吗?

时间:2013-02-14 17:32:01

标签: c# .net windows recaptcha

我想将它集成到Windows应用程序中。我正在使用Visual Studio 2010,它是一个C#Windows窗体应用程序。

1 个答案:

答案 0 :(得分:5)

    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;
        }
    }

使用它像:

string challenge = null; // you will need this to submit captcha answer
pictureBox1.Image = DownloadReCaptcha("reCaptcha site key", ref challenge);

如何找到钥匙?

在网页的HTML源代码中,您会找到类似

的内容

<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LexLsMSAAAAABUuI6bvUYfxaumgcu0vGiEFotDA"></script>

key = 6LexLsMSAAAAABUuI6bvUYfxaumgcu0vGiEFotDA