Java HTTP proxy and server authentication

Sample Java code for http proxy and server authentication.

Published: Tuesday, 11 September 2012

Downloading via HTTP

Use the following to perform a HTTP download in standard Java:

public class HttpDownloadSample {
    public static void main(String[] args) throws Exception {
        URI uri = new URI("http://example.com");
        InputStream inputStream = uri.toURL().openStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        for (String s = br.readLine(); s!=null; s = br.readLine()) {
            System.out.println(s);
        }
    }
}

This assumes the web site requires no authentication.

Authentication

This is used for HTTP Authentication. This is for the standard login popup which appears in your browser, not for websites with end user login forms.

There is a global variable you can set using:

Authenticator.setDefault(Authenticator a)

This has to be done BEFORE you start downloading.

Note that this is global! You can affect other applications running within the same JVM.

You are expected to have your own subclass of Authenticator. Once set, you should implement getPasswordAuthentication. As part of your implementation you can check where the request is coming from, by checking the other methods such as getRequestorType, getRequestingHost. The values could change for each request.

Alternatives

Consider Apache Commons HTTPClient