Learn how to securely route Java HTTP traffic through a QuotaGuard Shield proxy using native HttpURLConnection. Includes code examples.
Java JDK installed.
The qgpass wrapper downloaded and extracted into your working directory.
HttpURLConnection uses system properties for proxy configuration:
https.proxyHost=localhosthttps.proxyPort=8080
It doesn't handle proxy authentication headers as cleanly as OkHttp, so QGPass acts as a local proxy that handles the authentication to QuotaGuard Shield.
Be sure to set QUOTAGUARDSHIELD_URL to your HTTPS proxy URL from the QuotaGuard Dashboard.
docker build -t qg-shield-httpurlconnection-example .
docker run -e QUOTAGUARDSHIELD_URL=https://username:password@your-proxy.quotaguard.com:9294 qg-shield-httpurlconnection-exampleimport java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.URL;
public class HttpsTest {
public static void main(String[] args) throws Exception {
// Using localhost:8080 with QGPass
URI uri = URI.create("http://localhost:8080");
String proxyHost = uri.getHost();
int proxyPort = uri.getPort();
// Set Java system properties so all HTTPS calls go through the proxy
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", String.valueOf(proxyPort));
// Simple request to echo proxy IP address
URL testUrl = new URL("https://ip.quotaguard.com");
HttpURLConnection conn = (HttpURLConnection) testUrl.openConnection();
int responseCode = conn.getResponseCode();
System.out.println("Response code: " + responseCode);
// Print the response body
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
}# Start from a standard Java image
FROM eclipse-temurin:17-alpine
# Install curl
RUN apk add --no-cache curl
WORKDIR /app
# Copy Java file into the container
COPY HttpsTest.java .
# Fetch and unpack QGPass
RUN curl -sSL https://s3.amazonaws.com/quotaguard/qgpass-latest.tar.gz \
| tar xz -C /app/
# Compile Java class
RUN javac HttpsTest.java
# Run Java app behind QGPass
CMD ["bin/qgpass", "java", "HttpsTest"]