none
Google ReCaptcha Answer RRS feed

  • Question

  • Hello,

    I have that code for creating a recaptcha image from google:

    public Image CreateRecaptcha(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");
                    Thread.Sleep(150);
                    return Image.FromFile("captcha.jpg");
    
                }
                catch (Exception)
                {
                    challenge = null;
                    return null;
                }
            }
    It is working fine and I get the image.
    The only thing I need is to get the string with the answer for the image (the text that appears on it).
    Can someone help me getting the answer from that method?
    I believe it's related to the 'challenge' and 'key' but I just couldn't find what to do with it.
    Friday, March 9, 2018 10:41 AM

All replies

  • Although the following is for ASP.NET you can still learn from it.

    using Newtonsoft.Json;
    
    public class ReCaptchaClass
    {
        public static string Validate(string EncodedResponse)
        {
            var client = new System.Net.WebClient();
    
            string PrivateKey = "6LcH-v8SerfgAPlLLffghrITSL9xM7XLrz8aeory";
    
            var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse));
    
            var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptchaClass>(GoogleReply);
    
            return captchaResponse.Success;
        }
    
        [JsonProperty("success")]
        public string Success
        {
            get { return m_Success; }
            set { m_Success = value; }
        }
    
        private string m_Success;
        [JsonProperty("error-codes")]
        public List<string> ErrorCodes
        {
            get { return m_ErrorCodes; }
            set { m_ErrorCodes = value; }
        }
    
    
        private List<string> m_ErrorCodes;
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 9, 2018 12:30 PM
    Moderator