Simple HTTP requests via a Static IP in Java

    Table of contents

    This example assumes you have a QuotaGuard Static account and have set the QUOTAGUARDSTATIC_URL variable in your environment with your unique connection string.

    import java.net.*;
    import java.io.*;
    
    public class HelloWorld {
        public static void main(String []args) throws IOException {
            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();
        }
    }
    

    You can check other available proxy related JVM settings here.

    Please note that JVM disables proxy authentication for HTTPS tunneling by default, as per JDK changelog:

    core-libs/java.net
    Disable Basic authentication for HTTPS tunneling
    In some environments, certain authentication schemes may be undesirable when proxying HTTPS. Accordingly, the Basic authentication scheme has been deactivated, by default, in the Oracle Java Runtime, by adding Basic to the jdk.http.auth.tunneling.disabledSchemes networking property. Now, proxies requiring Basic authentication when setting up a tunnel for HTTPS will no longer succeed by default. If required, this authentication scheme can be reactivated by removingBasic from the jdk.http.auth.tunneling.disabledSchemes networking property, or by setting a system property of the same name to "" ( empty ) on the command line.
    Additionally, the jdk.http.auth.tunneling.disabledSchemes and jdk.http.auth.proxying.disabledSchemes networking properties, and system properties of the same name, can be used to disable other authentication schemes that may be active when setting up a tunnel for HTTPS, or proxying plain HTTP, respectively.
    JDK-8160838 (not public)
    

    To enable authentication for HTTPS connections you either can use SOCKS5 proxy or set additional JVM variable:

    System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
    

    Ready to Get Started?

    Get in touch or create a free trial account