Advertisement
Guest User

Untitled

a guest
Jun 7th, 2015
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. public string GetReCaptchaResponse(bool noScriptUrl)
  2.         {
  3.             if (string.IsNullOrEmpty(siteSource)) return null;
  4.  
  5.             // Sprawdź czy trzeba rozwiązać ReCaptche
  6.             bool isReCaptchaAvailable = (siteSource.Contains("recaptcha_response_field") && siteSource.Contains("recaptcha_challenge_field"));
  7.             string recaptchaResponseField;
  8.  
  9.             if (isReCaptchaAvailable)
  10.             {
  11.                 // Wyłuskaj klucz publiczny
  12.                 string publicKey = Regex.Match(siteSource, "challenge\\?k=(?<publicKey>[^\"']+)").Groups["publicKey"].Value.Trim();
  13.                 if (string.IsNullOrEmpty(publicKey)) return null;
  14.  
  15.                 string recaptchaSrc = string.Empty;
  16.                 if (noScriptUrl)
  17.                     recaptchaSrc = webHelper.DownloadStuff(ChallengeNoScriptUrl + publicKey);
  18.                 else
  19.                     recaptchaSrc = webHelper.DownloadStuff(ChallengeUrl + publicKey);
  20.  
  21.                 // Wyłuskaj kod 'challenge', który posłuży do wczytania obrazka
  22.                 string recaptchaChallengeField = string.Empty;
  23.                 if (!noScriptUrl)
  24.                 {
  25.                     recaptchaChallengeField =
  26.                         Challenge =
  27.                         Regex.Match(recaptchaSrc, "challenge : '(?<challenge>[^']+)'\\,").Groups["challenge"].Value.Trim
  28.                             ();
  29.                     if (string.IsNullOrEmpty(recaptchaChallengeField)) return null;
  30.                 }
  31.  
  32.                 string recaptchaImageUrl = string.Empty;
  33.                 if (noScriptUrl)
  34.                 {
  35.                     this.Challenge =
  36.                         Regex.Match(recaptchaSrc, "image\\?c\\=(?<challenge>[^\"]+)").Groups["challenge"].Value;
  37.                     recaptchaImageUrl = ImageUrl
  38.                                         + this.Challenge;
  39.                 }
  40.                 else
  41.                     recaptchaImageUrl = ImageUrl + recaptchaChallengeField;
  42.  
  43.                 // Pobierz obrazek
  44.                 Image recaptchaImage = Image.FromStream(new MemoryStream(webHelper.DownloadData(recaptchaImageUrl)));
  45.                 if (recaptchaImage.Width == 0) return null;
  46.  
  47.                 FormLoginCaptcha captchaForm = new FormLoginCaptcha(recaptchaImage, siteBriefDescription);
  48.                 captchaForm.ShowDialog();
  49.                 recaptchaResponseField = captchaForm.GetCaptchaText();
  50.             }
  51.             else return null;
  52.  
  53.             return recaptchaResponseField;
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement