---
title: "Java HttpClient Proxy Setup and HTTPS Workarounds"
description: "Use a static IP proxy from Java: HttpClient with ProxySelector and Basic auth, why it cannot use an HTTPS proxy endpoint, and the plain port 8080 workaround."
canonical: https://fixedmark.com/docs/java
updated: 2026-11-03
source: Fixedmark (https://fixedmark.com), BitMask LLP
---

HTTP clients

# Java HttpClient proxy setup

Java's built-in HttpClient cannot open TLS to a proxy, so it cannot use the Fixedmark https\:// proxy URL. This page shows the HttpClient setup for the plain proxy on port 8080 and the other options for Java apps.

Docs preview: Fixedmark is not live yet. Endpoints are issued at launch, and these pages describe planned launch behavior. Anything marked planned may change. [Join early access](https://fixedmark.com/early-access) to get yours first.

## The limitation

`java.net.http.HttpClient` takes a `ProxySelector` that returns a host and port. It always connects to that address over plain TCP and sends `CONNECT` in clear text. There is no setting for TLS to the proxy. `HttpURLConnection` has the same limit.

Your HTTPS traffic to the destination is still encrypted end to end. What is exposed is the `Proxy-Authorization` header, which carries your app ID and token.

## HttpClient setup for a plain listener

The same host as your HTTPS proxy URL serves a plain HTTP CONNECT proxy on port 8080 for clients like Java. The snippet reads the host and port from env vars.

**HttpClient**

```java
// Plain HTTP CONNECT proxy on port 8080, for clients without HTTPS proxy
// support. It sends the token in cleartext. Run the JVM with
//   -Djdk.http.auth.tunneling.disabledSchemes=
// or HttpClient refuses to send Basic credentials to the proxy.
import java.net.*;
import java.net.http.*;
import java.time.Duration;

public class Main {
  public static void main(String[] args) throws Exception {
    String host = System.getenv("FIXEDMARK_PROXY_HOST");
    int port = Integer.parseInt(System.getenv("FIXEDMARK_PROXY_PORT"));

    HttpClient client = HttpClient.newBuilder()
        .proxy(ProxySelector.of(new InetSocketAddress(host, port)))
        .authenticator(new Authenticator() {
          @Override
          protected PasswordAuthentication getPasswordAuthentication() {
            // Only answer the proxy, never a 401 from the destination.
            if (getRequestorType() != RequestorType.PROXY) return null;
            return new PasswordAuthentication(
                System.getenv("FIXEDMARK_APP_ID"),
                System.getenv("FIXEDMARK_TOKEN").toCharArray());
          }
        })
        .connectTimeout(Duration.ofSeconds(10))
        .build();

    HttpRequest req = HttpRequest.newBuilder(
        URI.create("https://api.partner.example/v1/orders")).build();
    HttpResponse<String> res =
        client.send(req, HttpResponse.BodyHandlers.ofString());
    System.out.println(res.statusCode());
  }
}
```

The `Authenticator` checks `getRequestorType()`. Without that check it would also answer a `401` from the destination with your Fixedmark token.

## Basic auth for tunnels

The JDK disables Basic authentication for HTTPS tunnels by default through the `jdk.http.auth.tunneling.disabledSchemes` property. With the default, HttpClient never sends your credentials and you get `407`. Set the property to an empty value on the command line, or in `JAVA_TOOL_OPTIONS` on platforms where you cannot change the start command.

## Options for Java

- **Plain HTTP proxy on port 8080.** Works with HttpClient as shown. It sends the token in cleartext, so give the token a destination allowlist so a leaked credential can only reach the APIs you list.
- **fm tunnel (planned).** Forwards a local port to a remote host through your static IPs over TLS. It suits databases and fixed TCP endpoints. See the [fm tunnel reference](https://fixedmark.com/docs/cli).
- **SOCKS5.** `java.net.Socket` and many JDBC drivers can use SOCKS5, but plain SOCKS5 on port 1080 also sends credentials unencrypted, and HttpClient does not support SOCKS proxies.

## JDBC drivers

Drivers built on `java.net.Socket` follow the JVM-wide `socksProxyHost` and `socksProxyPort` properties. Those properties apply to every socket in the JVM, not only the database. For a single database connection, the planned `fm tunnel` is the cleaner option. See [databases](https://fixedmark.com/docs/databases).

Tested with OpenJDK 26: HttpClient got `407` through a plain CONNECT proxy until Basic tunneling was re-enabled, then succeeded. It could not connect to the TLS-wrapped proxy port.

## Platform guides

- [Render](https://fixedmark.com/integrations/render)
- [Heroku](https://fixedmark.com/integrations/heroku)
- [AWS Lambda](https://fixedmark.com/integrations/aws-lambda)
- [Google Cloud Run](https://fixedmark.com/integrations/google-cloud-run)

## Frequently asked questions

### Can Java HttpClient use an HTTPS proxy?

No. java.net.http.HttpClient connects to proxies over plain TCP only. It cannot open TLS to a proxy, so it cannot use an https\:// proxy URL.

### Why does Java HttpClient return 407 even with an Authenticator?

Since Java 8u111 the JDK disables Basic authentication for HTTPS tunnels by default. Start the JVM with -Djdk.http.auth.tunneling.disabledSchemes= (empty) to allow it.

### What is the best option for Java at launch?

For databases, fm tunnel, which always uses TLS. For HTTP APIs, the plain HTTP proxy on port 8080 with a destination-restricted token. HttpClient cannot do TLS to a proxy, so it needs that port, which sends the token in cleartext.
