Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Dec 8, 1997

HTTP Authorization

Question:
I am writing an application that uses the URLConnection class to open a connection to a web site. However one of the sites I wish to access has web server access control enabled. If I am going through a browser I see a dialog box requiring a username and password. How can I provide this information to the server throught my application. I have seen some information regarding setRequestProperty() which sounds likely enough. Do I have to encode the username/password? Thanks for your help

Answer:
Many programmers like to use Java to automate access to network resources because it is so easy to write networking code with the Java networking classes. Accessing URL's through Java is as simple as creating a URLConnection class instance and fetching its input stream. However, not all URL's are publicly accessible and require authentication. If you look at the specification for HTTP, you will find that URL's requiring a username and password require an Authorization property to accompany the GET request. The format for the Authorization property is the concatenation of the username and password separated by a colon and then Base64 encoded.

How do we do this in Java? The URLConnection class has a method called setRequestProperty() which can be used to set the value of an HTTP request property. The only trick remaining is to Base64 encode the username and password. There are three ways to do this. You can write your own Base64 encoder, you can use a third-party Base64 encoder, or you can use an undocumented and unsupported class in the Sun JDK. I'll show you how to do it with the third method. But keep in mind that Sun makes no guarantees that classes in any of the sun.* packages will be present in future versions of their JDK. In Java 1.2 there will be a java.net.Authenticator class to make this task easier.

import java.io.*;
import java.net.*;
import sun.misc.BASE64Encoder;

public final class HTTPAuthorization {
  public static final void main(String[] args) {
    String address, username, password, key;
    BASE64Encoder encoder;
    URL url;
    URLConnection connection;
    BufferedReader input;

    address  = args[0];
    username = args[1];
    password = args[2];

    key = username + ":" + password;

    encoder = new BASE64Encoder();

    key = encoder.encode(key.getBytes());

    try {
      url = new URL(address);

      connection = url.openConnection();
      connection.setRequestProperty("Authorization", "Basic " + key);

      input = new BufferedReader(new InputStreamReader(
				       connection.getInputStream()));

      while((address = input.readLine()) != null)
	System.out.println(address);

    } catch(IOException e) {
      e.printStackTrace();
    }
  }
}
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date