Reading URLs protected with HTTP Authentication with Java

In Java, you can use URLConnection class to read from an URL. For example, to read the page from google.com, you can do something like this:

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

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL google = new URL("http://www.google.com/");
        URLConnection gc = google.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                gc.getInputStream()));
        String inputLine;

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

If the access to the URL is protected with authentication mechanism, IOException is thrown when you try to read from InputStream associated with URLConnection. In that case, you need to use Authenticator class from java.net package, that makes accessing password-protected URLs as easy as can be.

So, you simply install an Authenticator, with Authenticator.setDefault(). Then, when authentication is necessary, the installed Authenticator’s getPasswordAuthentication() method is called, and you return a PasswordAuthentication instance with the appropriate username and password.

The example code is shown below.

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

public class URLConnectionAuthReader {
    public static void main(String[] args) throws Exception {
        String username = "username";
        String password = "password";
        URL authurl = new URL("http://www.protectedurl.com/");

        Authenticator.setDefault(new MyAuthenticator(username, password));
        URLConnection ac = authurl.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                ac.getInputStream()));
        String inputLine;

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

    protected static class MyAuthenticator extends Authenticator {
        private String username, password;

        public MyAuthenticator(String user, String pwd) {
            username = user;
            password = pwd;
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password.toCharArray());
        }
    }
}

One Response to “Reading URLs protected with HTTP Authentication with Java”

  1. Marcus says:

    Does it seem a little odd to anyone else (and a bad idea) for this to be configured using a static / global method call and not directly attached to the URLConnection itself? This doesn’t seem like that scaleable of a method.

Leave a Reply