Captcha bypass tutorials

How to bypass captcha using Java

How to bypass captcha using Java: Code examples

Bypassing captchas for server testing and automation can be challenging, but with the right approach, it's possible to streamline the process effectively.

This article explores how recognition services can help bypass captchas.

Captchas bypass reasons

The primary goal of using recognition technologies is not to compromise security but to optimize automated workflows and improve accessibility. captchas often create barriers for users with disabilities, making it essential to develop inclusive solutions.

Additionally, captchas can pose a significant challenge for web scraping and automated testing. While this adds complexity to automation, understanding the right tools and techniques can make the process much more manageable.

Bypass captcha in JAVA

This article explores how to bypass captchas in Java using specialized libraries and third-party solving services. We’ll focus on integrating a Java package with the captcha solver API to solve reCAPTCHA, Arkose, Cloudflare, and other common captcha types. By implementing this solution, you can automate captcha handling with minimal effort, ensuring fast and for your bots.
The same approach can be used with other captcha solving services like SolveCaptcha (captcha solver API), allowing flexible integration based on your preferred service.

Installation

The script package can be installed via a package manager or manually.

The 2captcha-java library is available on Maven Central for easy integration.

For more details, check out the GitHub Java repository, where you’ll find libraries and modules to simplify working with the solver API.

Configuration

After installing the TwoCaptcha package, several configuration options can be adjusted to optimize its functionality based on your requirements. Below is a detailed description of all the necessary parameters available for customization.

Initializing a instance

To start using the 2Captcha solver in your Java application, create an instance of the TwoCaptcha class with your API key:

TwoCaptcha solver = new TwoCaptcha("YOUR_API_KEY");

You can also configure additional options to fine-tune the behavior of the solver instance:

solver.setSoftId(123);  // Your software ID from the 2Captcha software catalog  
solver.setCallback("https://your.site/result-receiver");  // URL for result delivery  
solver.setDefaultTimeout(120);  // General polling timeout (except reCAPTCHA)  
solver.setRecaptchaTimeout(600);  // Timeout for reCAPTCHA solving  
solver.setPollingInterval(10);  // Interval between API polling requests  

Options

Option Default Value Description
softId - Software ID obtained after publishing in the 2Captcha software catalog.
callback - URL where the recognition result will be sent. Must be registered in your account’s pingback settings.
defaultTimeout 120 Polling timeout (in seconds) for all captcha types except reCAPTCHA. Defines how long the solver will attempt to retrieve the result from the res.php API endpoint.
recaptchaTimeout 600 Polling timeout (in seconds) specifically for reCAPTCHA solving.
pollingInterval 10 Time interval (in seconds) between polling requests to res.php. Setting values below 5 seconds is not recommended.

Important notes

  • If a callback URL is set, the TwoCaptcha instance will not poll the API for the result. Instead, the captcha ID is returned, and the result will be sent to the callback URL when ready.
  • To retrieve the result manually, use the getResult() method after submitting a captcha request.

Solve captcha

When submitting an image-based captcha, you can provide additional options to help 2Captcha workers solve it more efficiently.

Options

Option Default Value Description
numeric 0 Defines if the captcha contains only numbers or other symbols (see API docs for details).
minLength 0 Minimum length of the answer.
maxLength 0 Maximum length of the answer.
phrase 0 Defines if the answer consists of multiple words.
caseSensitive 0 Specifies whether the answer is case-sensitive.
calc 0 Defines whether the captcha requires a mathematical calculation.
lang - Specifies the captcha language (see the list of supported languages).
hintImg - Provides an image with hints to assist captcha workers.
hintText - Provides a text hint or task description for captcha workers.

Basic example

Below is a basic example demonstrating how to use the 2Captcha solver with error handling:

Normal captcha = new Normal();
captcha.setFile("path/to/captcha.jpg");
captcha.setMinLen(4);
captcha.setMaxLen(20);
captcha.setCaseSensitive(true);
captcha.setLang("en");

try {
    solver.solve(captcha);
    System.out.println("Captcha solved: " + captcha.getCode());
} catch (Exception e) {
    System.out.println("Error occurred: " + e.getMessage());
}

Normal Captcha (Text-based)

To bypass a normal captcha (distorted text on an image), use the following method. This can also be applied to recognize any text within an image:

Normal captcha = new Normal();
captcha.setFile("path/to/captcha.jpg");
captcha.setNumeric(4);
captcha.setMinLen(4);
captcha.setMaxLen(20);
captcha.setPhrase(true);
captcha.setCaseSensitive(true);
captcha.setCalc(false);
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Type red symbols only");

Text-based captcha

For captchas that require answering a question in plain text:

Text captcha = new Text();
captcha.setText("If tomorrow is Saturday, what day is today?");
captcha.setLang("en");

reCAPTCHA v2

Use this method to solve reCAPTCHA v2 and obtain a token for bypassing the protection:

ReCaptcha captcha = new ReCaptcha();
captcha.setSiteKey("6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-");
captcha.setUrl("https://mysite.com/page/with/recaptcha");
captcha.setInvisible(true);
captcha.setAction("verify");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");

reCAPTCHA v3

This method solves reCAPTCHA v3 and returns a token:

ReCaptcha captcha = new ReCaptcha();
captcha.setSiteKey("6Le-wvkSVVABCPBMRTvw0Q4Muexq1bi0DJwx_mJ-");
captcha.setUrl("https://mysite.com/page/with/recaptcha");
captcha.setVersion("v3");
captcha.setAction("verify");
captcha.setScore(0.3);
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");

FunCaptcha (Arkose)

Solves FunCaptcha (Arkose Labs) and returns a token:

FunCaptcha captcha = new FunCaptcha();
captcha.setSiteKey("69A21A01-CC7B-B9C6-0F9A-E7FA06677FFC");
captcha.setUrl("https://mysite.com/page/with/funcaptcha");
captcha.setSUrl("https://client-api.arkoselabs.com");
captcha.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36");
captcha.setData("anyKey", "anyValue");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");

GeeTest

This method solves GeeTest puzzle captchas and returns a set of tokens as JSON:

GeeTest captcha = new GeeTest();
captcha.setGt("f2ae6cadcf7886856696502e1d55e00c");
captcha.setApiServer("api-na.geetest.com");
captcha.setChallenge("12345678abc90123d45678ef90123a456b");
captcha.setUrl("https://mysite.com/captcha.html");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");

KeyCaptcha

Solves KeyCaptcha using a token-based method:

KeyCaptcha captcha = new KeyCaptcha();
captcha.setUserId(10);
captcha.setSessionId("493e52c37c10c2bcdf4a00cbc9ccd1e8");
captcha.setWebServerSign("9006dc725760858e4c0715b835472f22");
captcha.setWebServerSign2("2ca3abe86d90c6142d5571db98af6714");
captcha.setUrl("https://www.keycaptcha.ru/demo-magnetic/");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");

Capy

Solves Capy puzzle captchas using a token-based method:

Capy captcha = new Capy();
captcha.setSiteKey("PUZZLE_Abc1dEFghIJKLM2no34P56q7rStu8v");
captcha.setUrl("https://www.mysite.com/captcha/");
captcha.setProxy("HTTPS", "login:password@IP_address:PORT");

Grid

The Grid method is also known as the "Old reCAPTCHA V2" method. It is used to solve captchas that require selecting specific areas of an image.

Grid captcha = new Grid();
captcha.setFile("path/to/captcha.jpg");
captcha.setRows(3);
captcha.setCols(3);
captcha.setPreviousId(0);
captcha.setCanSkip(false);
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Select all images with an Orange");

Rotate captcha

This method solves captchas that require rotating an object to a correct position (commonly used for FunCaptcha). It returns the rotation angle.

Rotate captcha = new Rotate();
captcha.setFile("path/to/captcha.jpg");
captcha.setAngle(40);
captcha.setLang("en");
captcha.setHintImg(new File("path/to/hint.jpg"));
captcha.setHintText("Put the images in the correct way up");

By using the appropriate method for each captcha type, 2Captcha enables seamless automation while handling various verification challenges efficiently.

Other methods

Additional valid methods used during the work of the main scripts.

Manual captcha submission and result retrieval

The send and getResult methods allow manual submission of captchas and polling for results.

Example

// Send captcha for solving
String captchaId = solver.send(captcha);

// Wait for 20 seconds before polling for the result
Thread.sleep(20 * 1000);

// Retrieve the captcha solution
String code = solver.getResult(captchaId);

Checking account balance

To check your 2Captcha account balance, use the balance() method:

// Retrieve current account balance
double balance = solver.balance();

Reporting captcha results

If a captcha answer is incorrect, it can be reported to 2Captcha using the report method.

Example: Reporting captcha accuracy

// Report a correctly solved captcha
solver.report(captcha.getId(), true);

// Report an incorrectly solved captcha
solver.report(captcha.getId(), false);

Reporting incorrect answers helps improve service accuracy and avoid being charged for faulty responses.

Error handling

Possible variants of standard errors returned by the service when processing requests.

If an error occurs during captcha solving, the solver throws an exception. Proper error handling ensures that your application can gracefully manage failures. It is recommended to use a try/catch block to handle different types of exceptions.

Example: Handling exceptions

try {
    solver.solve(captcha);
} catch (ValidationException e) {
    // Invalid parameters were passed (e.g., missing API key or incorrect captcha type)
    System.out.println("Validation error: " + e.getMessage());
} catch (NetworkException e) {
    // A network error occurred (e.g., timeout, connection issue)
    System.out.println("Network error: " + e.getMessage());
} catch (ApiException e) {
    // The API responded with an error (e.g., insufficient balance, invalid request)
    System.out.println("API error: " + e.getMessage());
} catch (TimeoutException e) {
    // The captcha was not solved within the expected time
    System.out.println("Timeout error: " + e.getMessage());
} catch (Exception e) {
    // General exception handler for any other unexpected errors
    System.out.println("An unexpected error occurred: " + e.getMessage());
}

Using proper error handling ensures that your application can detect and respond to issues such as invalid requests, API errors, or network failures without crashing.