Skip to content

Instantly share code, notes, and snippets.

@Spring3
Last active October 28, 2021 04:16

Revisions

  1. Spring3 revised this gist Jan 6, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -19,14 +19,14 @@ private String solveCaptcha(WebDriver driver) throws Exception{
    client.AddPostParam("key", captchaUserId);
    client.POST("http://2captcha.com/in.php");
    if (client.responseContains("OK")){
    setWaitInfo("Captcha image sent");
    System.out.println("Captcha image sent");
    }
    else{
    setWaitInfo(client.getRawBody());
    System.err.println(client.getRawBody());
    return null;
    }
    String key = client.getRawBody().substring(client.getRawBody().indexOf("|") + 1, client.getRawBody().length());
    setWaitInfo("Captcha waiting key :" + key);
    System.out.println("Captcha waiting key :" + key);

    do{
    client.Navigate(String.format("http://2captcha.com/res.php?key=%s&action=get&id=%s", captchaUserId, key));
    @@ -36,10 +36,10 @@ private String solveCaptcha(WebDriver driver) throws Exception{
    String captchaAnswer = null;
    if (client.responseContains("OK")){
    captchaAnswer = client.getRawBody().substring(client.getRawBody().indexOf("|") + 1, client.getRawBody().length());
    setWaitInfo("Captcha solved: " + captchaAnswer);
    System.out.println("Captcha solved: " + captchaAnswer);
    }else{
    setWaitInfo("Captcha was not solved");
    setWaitInfo(client.getRawBody());
    System.err.println("Captcha was not solved");
    System.err.println(client.getRawBody());
    }
    return captchaAnswer;
    }
  2. Spring3 revised this gist Jan 6, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,6 @@ private String solveCaptcha(WebDriver driver) throws Exception{
    }
    else{
    setWaitInfo(client.getRawBody());
    result = 0;
    return null;
    }
    String key = client.getRawBody().substring(client.getRawBody().indexOf("|") + 1, client.getRawBody().length());
    @@ -41,7 +40,6 @@ private String solveCaptcha(WebDriver driver) throws Exception{
    }else{
    setWaitInfo("Captcha was not solved");
    setWaitInfo(client.getRawBody());
    result = 0;
    }
    return captchaAnswer;
    }
  3. Spring3 revised this gist Jan 6, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    private String solveCaptcha(WebDriver driver) throws Exception{
    WebElement captchaChallenge = driver.findElement(By.id("recaptcha_challenge_image"));
    if (captchaChallenge != null){
    String imageURL = RequestHelper.MakeAbsoluteURL(client.getCurrentURL(), captchaChallenge.getAttribute("src"));
    String imageURL = captchaChallenge.getAttribute("src");
    InputStream in = new BufferedInputStream(new URL(imageURL).openStream());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    for (int i; (i = in.read()) != -1;){
  4. Spring3 created this gist Jan 6, 2016.
    47 changes: 47 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    //client = HttpClient
    //captchaUserId = user id from http://2captcha.com

    private String solveCaptcha(WebDriver driver) throws Exception{
    WebElement captchaChallenge = driver.findElement(By.id("recaptcha_challenge_image"));
    if (captchaChallenge != null){
    String imageURL = RequestHelper.MakeAbsoluteURL(client.getCurrentURL(), captchaChallenge.getAttribute("src"));
    InputStream in = new BufferedInputStream(new URL(imageURL).openStream());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    for (int i; (i = in.read()) != -1;){
    baos.write(i);
    }
    in.close();
    baos.flush();
    String base64Image = Base64.encodeBase64String(baos.toByteArray());
    baos.close();
    client.AddPostParam("body", base64Image);
    client.AddPostParam("method", "base64");
    client.AddPostParam("key", captchaUserId);
    client.POST("http://2captcha.com/in.php");
    if (client.responseContains("OK")){
    setWaitInfo("Captcha image sent");
    }
    else{
    setWaitInfo(client.getRawBody());
    result = 0;
    return null;
    }
    String key = client.getRawBody().substring(client.getRawBody().indexOf("|") + 1, client.getRawBody().length());
    setWaitInfo("Captcha waiting key :" + key);

    do{
    client.Navigate(String.format("http://2captcha.com/res.php?key=%s&action=get&id=%s", captchaUserId, key));
    Thread.sleep(1000);
    } while(client.responseContains("NOT_READY"));
    }
    String captchaAnswer = null;
    if (client.responseContains("OK")){
    captchaAnswer = client.getRawBody().substring(client.getRawBody().indexOf("|") + 1, client.getRawBody().length());
    setWaitInfo("Captcha solved: " + captchaAnswer);
    }else{
    setWaitInfo("Captcha was not solved");
    setWaitInfo(client.getRawBody());
    result = 0;
    }
    return captchaAnswer;
    }