Developers

Integrations

Guide

Java

Send requests from Java through a Crawlhub proxy with java.net.http.HttpClient.

Your Crawlhub connection

FieldValue
GatewayUse the assigned host and port for your client’s protocol.
Residential usernameCopy the username from your configured residential connection.
ISP / Datacenter usernameUse the username and port assigned to your product.

Scroll horizontally to read all columns.

Use the protocol supported by this client. The examples below use PROXY_SERVER or separate host/port fields for these connection values. Read the proxy parameter reference.

Prepare the client

You need Java 11 or newer. The built-in java.net.http client is enough; no extra library is needed.

Save the example as ExitIp.java and start it with java ExitIp.java.

Java turns off Basic auth for proxy tunnels by default. Without the jdk.http.auth.tunneling.disabledSchemes line, every https:// request fails with 407. You can also set it on the command line: java -Djdk.http.auth.tunneling.disabledSchemes="" ExitIp.java.

Put your proxy login in environment variables, not in the code. Copy the username and password from your dashboard.

Set your proxy login
export PROXY_USERNAME="your-proxy-username"
export PROXY_PASSWORD="your-proxy-password"

Connection example

The example connects to http://proxy.crawlhub.ai:8000 and prints the exit IP that the target sees.

Java proxy example
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class ExitIp {
    public static void main(String[] args) throws Exception {
        // Allow Basic auth to the proxy for https:// targets (CONNECT).
        // Set it before the first request.
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        String user = System.getenv("PROXY_USERNAME");
        String pass = System.getenv("PROXY_PASSWORD");

        HttpClient client = HttpClient.newBuilder()
            .proxy(ProxySelector.of(new InetSocketAddress("proxy.crawlhub.ai", 8000)))
            .authenticator(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    if (getRequestorType() != RequestorType.PROXY) return null;
                    return new PasswordAuthentication(user, pass.toCharArray());
                }
            })
            .connectTimeout(Duration.ofSeconds(10))
            .build();

        HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.ipify.org"))
            .timeout(Duration.ofSeconds(30))
            .build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println("Exit IP: " + response.body());
    }
}

Add a country or session

Add options to the end of the username. The password stays the same. __cc_us picks a US exit. __cc_us__sid_job01 picks a US exit and keeps the same IP while you reuse that session ID.

Username options
// US exit
String user = System.getenv("PROXY_USERNAME") + "__cc_us";

// Or a US exit that keeps the same IP for session job01
// String user = System.getenv("PROXY_USERNAME") + "__cc_us__sid_job01";

Put the country before the session. Read the proxy parameter reference for all options.

Reuse the client

Build one HttpClient for each proxy setting and share it across threads. It keeps connections open and reuses them.

The Authenticator returns one username. For a second country or session, build a second HttpClient with its own Authenticator.

Verify the connection

Run the example. It prints the exit IP, which is the address of the proxy, not your own. Run it again without a session ID and the IP can change.

Use a public HTTPS target on port 443. The gateway also forwards plain http:// on port 80. It refuses other ports and private or local addresses. Keep TLS verification on.

When it fails

407 means the proxy did not accept the login. Check the username, the password and the options you added. client.send throws an IOException. If every https:// request gets 407 although the login is right, check that the tunneling property is set to an empty value before the first request. A 407 can also mean that the trial ended or no traffic is left, so check your dashboard.

403 means the gateway refused the target, for example a port other than 80 or 443. 502 means the exit or the target did not answer. Retry after a short wait.

See Troubleshooting for other errors. Send your client version and the error text, without your password, to support.

Search documentation

Search guides, products and reference pages.