Learn how to securely route Java HTTP traffic through a QuotaGuard Shield proxy using the OkHttp library. Includes code examples.
Java JDK installed.
The qgpass wrapper downloaded and extracted into your working directory.
This example demonstrates how to use OkHttp with QuotaGuard Shield proxy through QGPass.
docker build -t qg-shield-okhttp-example .
docker run -e QUOTAGUARDSHIELD_URL=https://username:password@your-proxy.quotaguard.com:9294 qg-shield-okhttp-example
The application connects to QGPass (localhost:8080) which handles authentication and tunneling to the actual QuotaGuard Shield proxy. QGPass is required because QuotaGuard Shield proxies don't accept direct HTTPS tunnel connections from client applications.
Be sure to set QUOTAGUARDSHIELD_URL to your HTTPS proxy URL from the QuotaGuard Dashboard.
// --- HttpsTest.java ---
import okhttp3.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
public class HttpsTest {
public static void main(String[] args) throws Exception {
// Using localhost:8080 with QGPass
String proxyHost = "localhost";
int proxyPort = 8080;
// Create OkHttp client with proxy configuration
OkHttpClient client = new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.protocols(List.of(Protocol.HTTP_1_1))
.retryOnConnectionFailure(false)
.build();
// Simple request to echo proxy IP address
Request request = new Request.Builder()
.url("https://ip.quotaguard.com")
.build();
try (Response response = client.newCall(request).execute()) {
int responseCode = response.code();
System.out.println("Response code: " + responseCode);
// Print the response body
if (response.body() != null) {
System.out.println(response.body().string());
}
}
}
}
# --- Dockerfile ---
# OkHttp HTTPS Proxy Example
FROM eclipse-temurin:17-alpine
# Install curl and wget
RUN apk add --no-cache curl wget
WORKDIR /app
# Download OkHttp JAR and its dependencies
RUN wget https://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp/4.12.0/okhttp-4.12.0.jar -O okhttp.jar && \
wget https://repo1.maven.org/maven2/com/squareup/okio/okio-jvm/3.6.0/okio-jvm-3.6.0.jar -O okio.jar && \
wget https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.8.21/kotlin-stdlib-1.8.21.jar -O kotlin-stdlib.jar && \
wget https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.21/kotlin-stdlib-common-1.8.21.jar -O kotlin-stdlib-common.jar && \
wget https://repo1.maven.org/maven2/org/jetbrains/annotations/13.0/annotations-13.0.jar -O annotations.jar
# Copy Java file
COPY HttpsTest.java .
# Fetch and unpack QGPass (required for QuotaGuard Shield)
RUN curl -sSL https://s3.amazonaws.com/quotaguard/qgpass-latest.tar.gz | tar xz -C /app/
# Set classpath environment variable for cleaner command
ENV CLASSPATH=".:okhttp.jar:okio.jar:kotlin-stdlib.jar:kotlin-stdlib-common.jar:annotations.jar"
# Compile Java class
RUN javac -cp "$CLASSPATH" HttpsTest.java
# Run with QGPass (required for QuotaGuard Shield)
CMD ["bin/qgpass", "java", "HttpsTest"]