Tutorials

Solve CAPTCHAs with Java Using CaptchaAI

This tutorial shows how to integrate CaptchaAI into Java applications using java.net.http.HttpClient (Java 11+). No third-party dependencies required.

Requirements

Requirement Details
Java 11+ (HttpClient API)
Dependencies None (standard library)
CaptchaAI API key Get one here

CaptchaAI Client Class

import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.stream.Collectors;

public class CaptchaAI {

    private final String apiKey;
    private final String baseUrl = "https://ocr.captchaai.com";
    private final HttpClient httpClient;

    public CaptchaAI(String apiKey) {
        this.apiKey = apiKey;
        this.httpClient = HttpClient.newBuilder()
                .connectTimeout(Duration.ofSeconds(30))
                .build();
    }

    /**

     * Submit a CAPTCHA task and return the task ID.
     */
    public String submit(Map<String, String> params) throws Exception {
        params.put("key", apiKey);
        String query = buildQuery(params);
        String url = baseUrl + "/in.php?" + query;

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .GET()
                .build();

        HttpResponse<String> response = httpClient.send(
                request, HttpResponse.BodyHandlers.ofString());
        String body = response.body();

        if (!body.startsWith("OK|")) {
            throw new RuntimeException("Submit failed: " + body);
        }

        return body.split("\\|", 2)[1];
    }

    /**

     * Poll for the result with a timeout in seconds.
     */
    public String poll(String taskId, int timeoutSeconds) throws Exception {
        long deadline = System.currentTimeMillis() + (timeoutSeconds * 1000L);
        String query = buildQuery(Map.of(
                "key", apiKey,
                "action", "get",
                "id", taskId
        ));
        String url = baseUrl + "/res.php?" + query;

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .GET()
                .build();

        while (System.currentTimeMillis() < deadline) {
            Thread.sleep(5000);

            HttpResponse<String> response = httpClient.send(
                    request, HttpResponse.BodyHandlers.ofString());
            String body = response.body();

            if ("CAPCHA_NOT_READY".equals(body)) {
                continue;
            }

            if (body.startsWith("OK|")) {
                return body.split("\\|", 2)[1];
            }

            throw new RuntimeException("Solve failed: " + body);
        }

        throw new RuntimeException("Timeout after " + timeoutSeconds
                + "s for task " + taskId);
    }

    /**

     * Submit and poll in one call.
     */
    public String solve(Map<String, String> params) throws Exception {
        String taskId = submit(params);
        return poll(taskId, 300);
    }

    /**

     * Check account balance.
     */
    public double getBalance() throws Exception {
        String query = buildQuery(Map.of(
                "key", apiKey,
                "action", "getbalance"
        ));
        String url = baseUrl + "/res.php?" + query;

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .GET()
                .build();

        HttpResponse<String> response = httpClient.send(
                request, HttpResponse.BodyHandlers.ofString());

        return Double.parseDouble(response.body());
    }

    private String buildQuery(Map<String, String> params) {
        return params.entrySet().stream()
                .map(e -> URLEncoder.encode(e.getKey(), StandardCharsets.UTF_8)
                        + "="
                        + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
                .collect(Collectors.joining("&"));
    }
}

Solve reCAPTCHA v2

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws Exception {
        CaptchaAI solver = new CaptchaAI(System.getenv("CAPTCHAAI_API_KEY"));

        Map<String, String> params = new HashMap<>();
        params.put("method", "userrecaptcha");
        params.put("googlekey", "6Le-wvkS...");
        params.put("pageurl", "https://example.com");

        String token = solver.solve(params);
        System.out.println("Token: " + token);
    }
}

Solve reCAPTCHA v3

Map<String, String> params = new HashMap<>();
params.put("method", "userrecaptcha");
params.put("googlekey", "6Le-wvkS...");
params.put("pageurl", "https://example.com");
params.put("version", "v3");
params.put("action", "login");

String token = solver.solve(params);

Solve Cloudflare Turnstile

Map<String, String> params = new HashMap<>();
params.put("method", "turnstile");
params.put("sitekey", "0x4AAAAA...");
params.put("pageurl", "https://example.com");

String token = solver.solve(params);

Solve Image CAPTCHAs

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;

byte[] imageBytes = Files.readAllBytes(Path.of("captcha.png"));
String imageB64 = Base64.getEncoder().encodeToString(imageBytes);

Map<String, String> params = new HashMap<>();
params.put("method", "base64");
params.put("body", imageB64);

String text = solver.solve(params);
System.out.println("Text: " + text);

Concurrent Solving with CompletableFuture

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ParallelSolver {
    public static void main(String[] args) throws Exception {
        CaptchaAI solver = new CaptchaAI(System.getenv("CAPTCHAAI_API_KEY"));
        ExecutorService executor = Executors.newFixedThreadPool(10);

        List<String> pages = List.of(
                "https://example.com/page1",
                "https://example.com/page2",
                "https://example.com/page3"
        );

        List<CompletableFuture<String>> futures = new ArrayList<>();
        for (String page : pages) {
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                try {
                    Map<String, String> params = new HashMap<>();
                    params.put("method", "userrecaptcha");
                    params.put("googlekey", "6Le-wvkS...");
                    params.put("pageurl", page);
                    return solver.solve(params);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, executor);
            futures.add(future);
        }

        for (int i = 0; i < futures.size(); i++) {
            try {
                String token = futures.get(i).get();
                System.out.printf("Page %d: solved (%d chars)%n", i, token.length());
            } catch (Exception e) {
                System.err.printf("Page %d: %s%n", i, e.getMessage());
            }
        }

        executor.shutdown();
    }
}

Spring Boot Integration

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CaptchaConfig {

    @Bean
    public CaptchaAI captchaAI(@Value("${captchaai.api-key}") String apiKey) {
        return new CaptchaAI(apiKey);
    }
}
# application.yml
captchaai:
  api-key: ${CAPTCHAAI_API_KEY}
import org.springframework.web.bind.annotation.*;

@RestController
public class FormController {

    private final CaptchaAI solver;

    public FormController(CaptchaAI solver) {
        this.solver = solver;
    }

    @PostMapping("/submit")
    public Map<String, Object> submit(@RequestBody Map<String, String> body)
            throws Exception {
        Map<String, String> params = new HashMap<>();
        params.put("method", "userrecaptcha");
        params.put("googlekey", body.get("siteKey"));
        params.put("pageurl", body.get("pageUrl"));

        String token = solver.solve(params);
        return Map.of("success", true, "tokenLength", token.length());
    }
}

Troubleshooting

Error Cause Fix
java.net.ConnectException Network issue Check firewall and DNS
Submit failed: ERROR_WRONG_USER_KEY Bad API key Copy key from dashboard
NumberFormatException on balance Unexpected response Check key validity
Timeout after 300s Slow solve or network Increase timeout; retry

FAQ

Does this work with Java 8?

The HttpClient API requires Java 11+. For Java 8, use HttpURLConnection or Apache HttpClient with the same API parameters.

Can I use this with Maven/Gradle?

Yes. The code has no external dependencies. Add it as a source file or package it in a local module.

Is the CaptchaAI class thread-safe?

Yes. HttpClient is thread-safe. Use a single CaptchaAI instance across threads.

Discussions (0)

No comments yet.

Related Posts

API Tutorials Java CompletableFuture + CaptchaAI: Async CAPTCHA Pipeline
Build an async CAPTCHA solving pipeline in Java using Completable Future — parallel submission, non-blocking polling, and result aggregation with Captcha AI.

Build an async CAPTCHA solving pipeline in Java using Completable Future — parallel submission, non-blocking p...

Automation All CAPTCHA Types Java
Mar 07, 2026
DevOps & Scaling Ansible Playbooks for CaptchaAI Worker Deployment
Deploy and manage Captcha AI workers with Ansible — playbooks for provisioning, configuration, rolling updates, and health checks across your server fleet.

Deploy and manage Captcha AI workers with Ansible — playbooks for provisioning, configuration, rolling updates...

Automation Python All CAPTCHA Types
Apr 07, 2026
DevOps & Scaling Blue-Green Deployment for CAPTCHA Solving Infrastructure
Implement blue-green deployments for CAPTCHA solving infrastructure — zero-downtime upgrades, traffic switching, and rollback strategies with Captcha AI.

Implement blue-green deployments for CAPTCHA solving infrastructure — zero-downtime upgrades, traffic switchin...

Automation Python All CAPTCHA Types
Apr 07, 2026
Troubleshooting CaptchaAI API Error Handling: Complete Decision Tree
Complete decision tree for every Captcha AI API error.

Complete decision tree for every Captcha AI API error. Learn which errors are retryable, which need parameter...

Automation Python All CAPTCHA Types
Mar 17, 2026
Tutorials Using Fiddler to Inspect CaptchaAI API Traffic
How to use Fiddler Everywhere and Fiddler Classic to capture, inspect, and debug Captcha AI API requests and responses — filters, breakpoints, and replay for tr...

How to use Fiddler Everywhere and Fiddler Classic to capture, inspect, and debug Captcha AI API requests and r...

Automation Python All CAPTCHA Types
Mar 05, 2026
Tutorials CAPTCHA Handling in Mobile Apps with Appium
Handle CAPTCHAs in mobile app automation using Appium and Captcha AI — extract Web sitekeys, solve, and inject tokens on Android and i OS.

Handle CAPTCHAs in mobile app automation using Appium and Captcha AI — extract Web View sitekeys, solve, and i...

Automation Python All CAPTCHA Types
Feb 13, 2026
Tutorials Streaming Batch Results: Processing CAPTCHA Solutions as They Arrive
Process CAPTCHA solutions the moment they arrive instead of waiting for tasks to complete — use async generators, event emitters, and callback patterns for stre...

Process CAPTCHA solutions the moment they arrive instead of waiting for all tasks to complete — use async gene...

Automation Python All CAPTCHA Types
Apr 07, 2026
Reference CaptchaAI CLI Tool: Command-Line CAPTCHA Solving and Testing
A reference for building and using a Captcha AI command-line tool — solve CAPTCHAs, check balance, test parameters, and integrate with shell scripts and CI/CD p...

A reference for building and using a Captcha AI command-line tool — solve CAPTCHAs, check balance, test parame...

Automation Python All CAPTCHA Types
Feb 26, 2026
DevOps & Scaling Auto-Scaling CAPTCHA Solving Workers
Build auto-scaling CAPTCHA solving workers that adjust capacity based on queue depth, balance, and solve rates.

Build auto-scaling CAPTCHA solving workers that adjust capacity based on queue depth, balance, and solve rates...

Automation Python All CAPTCHA Types
Mar 23, 2026
Tutorials Pytest Fixtures for CaptchaAI API Testing
Build reusable pytest fixtures to test CAPTCHA-solving workflows with Captcha AI.

Build reusable pytest fixtures to test CAPTCHA-solving workflows with Captcha AI. Covers mocking, live integra...

Automation Python reCAPTCHA v2
Apr 08, 2026
Tutorials GeeTest Token Injection in Browser Automation Frameworks
how to inject Gee Test v 3 solution tokens into Playwright, Puppeteer, and Selenium — including the three-value response, callback triggering, and form submissi...

Learn how to inject Gee Test v 3 solution tokens into Playwright, Puppeteer, and Selenium — including the thre...

Automation Python Testing
Jan 18, 2026