Setup a Static IP for Java HTTPS POST Requests

Learn how to route Java HTTP POST requests through a QuotaGuard Static IP proxy. Includes code examples.

Prerequisites

N/A

Instructions

Java HTTPS GET Request QuotaGuard Static Example

Be sure to set QUOTAGUARDSTATIC_URL to your Connection URL from the QuotaGuard Dashboard.

Test in Docker
docker build -t qg-static-java-get-example .
docker run -e QUOTAGUARDSTATIC_URL=... qg-static-java-get-example

Code Samples

Main.java
import java.net.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        URL proxyUrl = new URL(System.getenv("QUOTAGUARDSTATIC_URL"));
        String userInfo = proxyUrl.getUserInfo();
        String user = userInfo.substring(0, userInfo.indexOf(':'));
        String password = userInfo.substring(userInfo.indexOf(':') + 1);

        URLConnection conn = null;
        System.setProperty("http.proxyHost", proxyUrl.getHost());
        System.setProperty("http.proxyPort", Integer.toString(proxyUrl.getPort()));

        Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, password.toCharArray());
                }
            });

        URL url = new URL("http://ip.quotaguard.com");
        conn = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);

        in.close();
    }
}
Docker File
FROM openjdk:latest

WORKDIR /app/

COPY Main.java Main.java

RUN javac Main.java

ENTRYPOINT [ "java", "Main" ]