Java Captcha Solver
Solve image CAPTCHAs in Java with OkHttp or HttpClient. FastCaptcha returns the answer in 0.3–0.7 seconds with 95% accuracy — no proprietary SDK required.
Java Quick Start
OkHttp (recommended)
Add to pom.xml:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
import okhttp3.*;
import org.json.JSONObject;
import java.io.File;
public class CaptchaSolver {
private static final String API_KEY = "YOUR_API_KEY";
private static final OkHttpClient client = new OkHttpClient();
public static String solve(String imagePath) throws Exception {
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image", "captcha.png",
RequestBody.create(new File(imagePath),
MediaType.parse("image/png")))
.build();
Request request = new Request.Builder()
.url("https://fastcaptcha.org/api/v1/ocr/")
.addHeader("X-API-Key", API_KEY)
.post(body)
.build();
try (Response response = client.newCall(request).execute()) {
JSONObject json = new JSONObject(response.body().string());
if (json.getBoolean("success")) {
return json.getString("text");
}
throw new RuntimeException("API error: " + json.getString("error"));
}
}
}
Java HttpClient (Java 11+, no deps)
import java.net.http.*;
import java.net.URI;
import java.nio.file.*;
import java.util.UUID;
public class CaptchaSolver {
private static final String API_KEY = "YOUR_API_KEY";
private static final HttpClient client = HttpClient.newHttpClient();
public static String solve(Path imagePath) throws Exception {
String boundary = UUID.randomUUID().toString();
byte[] imageBytes = Files.readAllBytes(imagePath);
String body =
"--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"image\"; filename=\"captcha.png\"\r\n" +
"Content-Type: image/png\r\n\r\n";
byte[] prefix = body.getBytes();
byte[] suffix = ("\r\n--" + boundary + "--\r\n").getBytes();
byte[] full = new byte[prefix.length + imageBytes.length + suffix.length];
System.arraycopy(prefix, 0, full, 0, prefix.length);
System.arraycopy(imageBytes, 0, full, prefix.length, imageBytes.length);
System.arraycopy(suffix, 0, full, prefix.length + imageBytes.length, suffix.length);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://fastcaptcha.org/api/v1/ocr/"))
.header("X-API-Key", API_KEY)
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
.POST(HttpRequest.BodyPublishers.ofByteArray(full))
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
// Parse JSON (use org.json or Gson)
// JSONObject json = new JSONObject(response.body());
// return json.getString("text");
return response.body(); // parse as needed
}
}
With retry & error handling
public static String solveWithRetry(String imagePath) throws Exception {
int maxRetries = 4;
for (int attempt = 0; attempt < maxRetries; attempt++) {
try {
return solve(imagePath);
} catch (RuntimeException e) {
String msg = e.getMessage();
boolean retriable = msg.contains("429") || msg.contains("500") || msg.contains("503");
if (retriable && attempt < maxRetries - 1) {
long delay = (long) Math.min(Math.pow(2, attempt) * 1000, 32000);
Thread.sleep(delay);
} else {
throw e;
}
}
}
throw new RuntimeException("Max retries exceeded");
}
⚡
0.3s avg
Response time on text-based image CAPTCHAs
🎯
95% accuracy
Trained on millions of CAPTCHA images
🆓
100 free credits
No credit card needed to start