Scarnox · Blog

HttpClient के साथ Java में captcha solver: working उदाहरण (2026)

Java में captcha solver एक HTTP call है। Java 11 और उसके बाद से java.net.http.HttpClient पहले से शामिल है, इसलिए इस guide के लिए केवल Gson की ज़रूरत है, JSON के लिए। आप Scarnox API को sitekey और page URL भेजते हैं, solution.token पढ़ते हैं, और token को site के form में submit करते हैं। यह guide PopularCaptcha कवर करती है, जिसे Scarnox आज proxyless आधार पर solve करता है।

मुफ़्त ट्रायल क्रेडिट से शुरू करें, कार्ड की ज़रूरत नहीं और किसी और चीज़ पर इस्तेमाल करने से पहले अपने किसी page पर code आज़माएँ।

ज़रूरतें

Solver class

Class एक HttpClient रखती है जो cookies सँभालता है, ताकि session form submit तक चले। Solve method Bearer header सेट करता है, JSON body भेजता है, और errorId जाँचता है:

// ScarnoxSolver.java
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.net.CookieManager;
import java.net.CookiePolicy;
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.LinkedHashMap;
import java.util.Map;

public class ScarnoxSolver {
    private static final String API_URL = "https://api.scarnox.com/api/tasks/create";
    private static final Gson GSON = new Gson();

    private final String apiKey;
    private final HttpClient http = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(10))
            .cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ALL))
            .build();

    public ScarnoxSolver(String apiKey) {
        this.apiKey = apiKey;
    }

    /** token लौटाता है। token तैयार होने तक call रुकती है। */
    public String solve(String sitekey, String pageUrl, boolean invisible) throws Exception {
        Map<String, Object> body = new LinkedHashMap<>();
        body.put("captcha_type", "PopularCaptchaTaskProxyless");
        body.put("site_key", sitekey);
        body.put("site_url", pageUrl);
        if (invisible) {
            body.put("is_invisible", true);
        }

        HttpRequest request = HttpRequest.newBuilder(URI.create(API_URL))
                .timeout(Duration.ofSeconds(185))
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(GSON.toJson(body)))
                .build();

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

        JsonObject data;
        try {
            data = JsonParser.parseString(response.body()).getAsJsonObject();
        } catch (RuntimeException e) {
            throw new IllegalStateException("HTTP " + response.statusCode() + ": not JSON");
        }

        if (data.get("errorId").getAsInt() != 0) {
            throw new IllegalStateException(
                    data.get("errorCode").getAsString() + ": "
                    + data.get("errorDescription").getAsString());
        }

        return data.getAsJsonObject("solution").get("token").getAsString();
    }

    /** token को site के form पर POST करता है और HTTP status लौटाता है। */
    public int submit(String pageUrl, String email, String token) throws Exception {
        // पहले page लोड करें ताकि session cookie सेट हो जाए।
        http.send(HttpRequest.newBuilder(URI.create(pageUrl)).GET().build(),
                HttpResponse.BodyHandlers.discarding());

        String form = "email=" + enc(email)
                + "&h-captcha-response=" + enc(token)
                + "&g-recaptcha-response=" + enc(token);

        HttpRequest request = HttpRequest.newBuilder(URI.create(pageUrl))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .POST(HttpRequest.BodyPublishers.ofString(form))
                .build();

        return http.send(request, HttpResponse.BodyHandlers.discarding()).statusCode();
    }

    private static String enc(String value) {
        return URLEncoder.encode(value, StandardCharsets.UTF_8);
    }
}

Authorization header में Bearer prefix इस्तेमाल होता है। Request का timeout 185 सेकंड है, क्योंकि API token तैयार होने तक रुकती है। कोई polling step नहीं है।

मुख्य program

Main method कुछ retries के साथ solve करती है, फिर token submit करती है:

// Main.java
public class Main {
    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("SCARNOX_API_KEY");
        if (apiKey == null || apiKey.isBlank()) {
            throw new IllegalStateException("Set SCARNOX_API_KEY");
        }

        String pageUrl = "https://example.com/signup";
        String sitekey = "YOUR-SITEKEY-UUID";
        ScarnoxSolver solver = new ScarnoxSolver(apiKey);

        String token = null;
        for (int attempt = 1; attempt <= 3 && token == null; attempt++) {
            try {
                token = solver.solve(sitekey, pageUrl, false);
            } catch (IllegalStateException e) {
                System.err.println("Attempt " + attempt + " failed: " + e.getMessage());
                Thread.sleep(attempt * 1000L);
            }
        }
        if (token == null) {
            throw new IllegalStateException("No token after 3 attempts");
        }

        int status = solver.submit(pageUrl, "you@example.com", token);
        System.out.println("Status: " + status);
    }
}

चलाएँ:

export SCARNOX_API_KEY=scarnox_YOURAPIKEY
javac -cp gson-2.10.1.jar *.java
java -cp .:gson-2.10.1.jar Main

यह ढाँचा ऐसा क्यों है

हर solver के लिए एक client। HttpClient thread-safe है और उसे दोबारा इस्तेमाल करने के लिए बनाया गया है। हर request पर नया बनाने से connections बर्बाद होते हैं।

Cookies चालू। Client पर CookieManager वह session रखता है जिसने form लोड किया था, ताकि token उसी cookie के साथ जाए जिसकी site उम्मीद करती है।

स्पष्ट error जाँच। API सफलता पर errorId को 0 लौटाती है। कोई और मान errorCode और errorDescription के साथ आता है, और solver उसी text के साथ throw करता है। Token solution.token पर है।

दोनों captcha fields। Form को h-captcha-response और g-recaptcha-response दोनों मिलते हैं। ज़्यादातर sites इनमें से कोई एक पढ़ती हैं।

समय

Token लगभग दो मिनट में expire होता है। Solve ठीक submit से पहले करें, लंबे job की शुरुआत में नहीं। captcha token expiry बताती है कि window का आकार कैसे तय करें, और expired token को reject हुए token से कैसे अलग करें।

Invisible widgets

जब widget invisible हो, तो solve को तीसरे argument के रूप में true पास करें। तब request में is_invisible: true जुड़ जाता है। अगर page को token के साथ एक JavaScript callback चाहिए, तो server-side client उसे ट्रिगर नहीं कर सकता, और browser automation tool बेहतर विकल्प है। invisible captcha guide पैटर्न सूचीबद्ध करती है।

Error handling

Retries अस्थायी failures को कवर करते हैं। उन errors पर retry बंद करें जो नहीं बदलेंगी, जैसे गलत sitekey, या ऐसा page URL जिस पर widget नहीं चलता, और उन्हें समीक्षा के लिए लॉग करें। पूरी policy, जिसमें वे codes शामिल हैं जिन पर रुकना है, captcha solver error handling में है।

ज़िम्मेदारी से scrape करें

अगर program सार्वजनिक pages scrape करता है, तो पहले robots.txt और site के terms जाँचें। Requests के बीच delay जोड़ें, और केवल वही personal data रखें जिसकी ज़रूरत है। कानूनी पृष्ठभूमि के लिए क्या web scraping legal है देखें।

कीमत

आप हर सफल solve के लिए भुगतान करते हैं, और विफल solves मुफ़्त हैं। दरों के लिए मौजूदा pricing देखें। शुरू करने के लिए मुफ़्त ट्रायल क्रेडिट से शुरू करें, कार्ड की ज़रूरत नहीं।

अगले कदम