Extended Example
Home ] Up ] Reader Example ] Writer Example ] [ Extended Example ] Posting Form Data ]

 

 

Here's a more extended example of the use of URLConnection.

To learn more about Basic Authorization in HTTP, you can go to:

The passing of Basic Authorization credentials (username and password) involves Base64 encoding.  
You can learn more about Base64 encoding at http://en.wikipedia.org/wiki/Base64 .

package networking;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * Class to connect to a URL and display the response header 
 * data and first ten lines of the the requested data.
 *
 * (Based on the class supplied in Core Java 2, Vol II,
 *  Advanced Concepts, by Horstmann & Cornell, Prentice-Hall, 2005)
 */
public class URLConnectionTest
{
  /**
   * Main entry point
   * @param args the command line arguments
   *             args[0] contains the URL (Defaults to http://www.rivier.edu/)
   *             args[1] & args[2] contain an optional
   *                     username and password.
   */
  public static void main(String[] args)
  {
    try
    {
      String urlString;
      if (args.length > 0)
        urlString = args[0];
      else
        urlString = "http://www.rivier.edu/";
      URL url = new URL(urlString);
      URLConnection connection = url.openConnection();
      
      // Supply username & password, if supplied
      if (args.length > 2)
      {
        // For an explanation of passing credentials for basic authorization, 
        // see "HTTP Authentication: Basic and Digest Access Authentication" 
        // http://www.ietf.org/rfc/rfc2617.txt
        // The username and password are separated by a colon character, 
        // and the entire string is then Base64 encoded.
        String username = args[1];
        String password = args[2];
        String input = username + ":" + password;
        String encoding = base64Encode(input);
        connection.setRequestProperty("Authorization", "Basic " + encoding);
      }
      
      System.out.println(">>>>");
      System.out.println("Connecting to: " + urlString);
      
      connection.connect();
      
      System.out.println("Connected...");
      
      // Print the Method line
      System.out.println("----------Method--------------");
      System.out.println("[0] " + connection.getHeaderField(0));
      
      // Print header fields
      System.out.println("----------Response Headers----");
      for (int line = 1; ; line++)
      {
        String header = connection.getHeaderField(line);
        if (header == null)
          break;
        System.out.println("[" + line + "] " + 
                           connection.getHeaderFieldKey(line) + ": " +
                           header
                          );
      }
      
      // Print how many header lines (subtract one for the 0th entry, 
      // which is the Method line).
      Map<String, List<String>> headers = connection.getHeaderFields();
      System.out.println("There were " + (headers.size() - 1) + " header lines");
      
      // Print convenience function outputs
      
      System.out.println("----------Convenience functions----");
      System.out.println("getContentType: " + connection.getContentType());
      System.out.println("getContentLength: " + connection.getContentLength());
      System.out.println("getContentEncoding: " + connection.getContentEncoding());
      System.out.println("getDate: " + connection.getDate());
      System.out.println("getExpiration: " + connection.getExpiration());
      System.out.println("getLastModified: " + connection.getLastModified());

      Scanner in = new Scanner(connection.getInputStream());
      
      // Print first ten lines of contents 
      
      System.out.println("----------Content--------");
      for (int line = 1; in.hasNextLine() && line <= 10; line++)
      {
        System.out.println("[" + line + "] " + in.nextLine());
      }
      if (in.hasNextLine())
      {
        System.out.println(". . .");
      }
    }
    catch (IOException ioe)
    {
      ioe.printStackTrace();
    }
  }
  
  /**
   * Computes the Base64 encoding of a string
   * @param string the string
   * @return the Base64 encoding of string
   */
  public static String base64Encode(String string)
  {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    Base64OutputStream out = new Base64OutputStream(bytesOut);
    try
    {
      out.write(string.getBytes());
      out.flush();
    }
    catch (IOException ioe)
    {
      // Do nothing
    }
    return bytesOut.toString();
  }
}
package networking;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * This class implements a stream filter that converts
 * a stream of bytes to their Base64 encoding.
 *
 * Base64 encoding encodes 3 bytes into 4 characters.
 * |11111122|22223333|444444|
 * Each set of 6 bits is encoded according to the m_toBase64 map.
 * If the number of input bytes is not a multiple of 3, then the 
 * last group of characters is padded with one or two = signs.
 * Each output line is at most 76 characters.
 *
 * (Based on the class supplied in Core Java 2, Vol II,
 *  Advanced Concepts, by Horstmann & Cornell, Prentice-Hall, 2005)
 */
public class Base64OutputStream extends FilterOutputStream
{
  /**
   * Constructor
   * @param out the stream to filter
   */
  public Base64OutputStream(OutputStream out)
  {
    super(out);
  }
  
  /**
   * Writes the specified <code>byte</code> to this output stream. 
   * <p>
   * @param      c the <code>byte</code>.
   */
  public void write(int c) throws IOException
  {
    m_inbuf[m_i] = c;
    m_i++;
    if (m_i == 3)
    {
      super.write( m_toBase64[(m_inbuf[0] & 0xFC) >> 2] );
      super.write( m_toBase64[((m_inbuf[0] & 0x03) << 4) |
                              ((m_inbuf[1] & 0xF0) >> 4)] );
      super.write( m_toBase64[((m_inbuf[1] & 0x0F) << 2) |
                              ((m_inbuf[2] & 0xC0) >> 6)] );
      super.write( m_toBase64[m_inbuf[2] & 0x3F] );
      m_col += 4;
      m_i = 0;
      if (m_col >= 76)
      {
        super.write('\n');
        m_col = 0;
      }
    }
  }
  
  /**
   * Flushes this output stream and forces any buffered output bytes 
   * to be written out to the stream. 
   */
  public void flush() throws IOException
  {
    if (m_i == 1)
    {
      super.write( m_toBase64[(m_inbuf[0] & 0xFC) >> 2] );
      super.write( m_toBase64[(m_inbuf[0] & 0x03) << 4] );
      super.write('=');
      super.write('=');
    }
    else if (m_i == 2)
    {
      super.write( m_toBase64[(m_inbuf[0] & 0xFC) >> 2] );
      super.write( m_toBase64[((m_inbuf[0] & 0x03) << 4) |
                              ((m_inbuf[1] & 0xF0) >> 4)] );
      super.write( m_toBase64[(m_inbuf[1] & 0x0F) << 2] );
      super.write('=');
    }
  }
  
  /////// Private data /////////
  private static char[] m_toBase64 =
  {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
    'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
    'u', 'v', 'w', 'x', 'y', 'z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '+', '/'
  };

  public void write(byte[] b) throws IOException
  {
  }
  
  private int m_col = 0;
  private int m_i = 0;
  private int[] m_inbuf = new int[3];
}

This example connects to a specified URL (by default, it connects to http://www.rivier.edu/), and prints out information returned by that URL.

For example, when connected to http://www.rivier.edu/, it produces the following output:

 
The page was last updated February 19, 2008