Java Network Programming, Part 1: URLs, URLConnections, and InetAddresses

Java Network Programming, Part 1: URLs, URLConnections, and InetAddresses

Elliotte Rusty Harold

elharo@metalab.unc.edu

http://metalab.unc.edu/javafaq/slides/


Please turn off all


We will learn how Java handles


I assume you


Applet Network Security Restrictions


Some Background


Hosts


Internet addresses


Domain Name System (DNS)


The InetAddress Class


Creating InetAddresses


The getByName() factory method


Other ways to create InetAddress objects


Getter Methods


Utility Methods


Ports


Protocols


IETF RFCs


W3C Standards


W3C Standards


URLs


Example URLs


The Pieces of a URL


The java.net.URL class


Content and Protocol Handlers


Finding Protocol Handlers


Supported Protocols


URL Constructors


Constructing URL Objects

try {
  URL u = new URL("http://www.poly.edu/fall97/grad.html#cs");
}
catch (MalformedURLException e) {}

Constructing URL Objects in Pieces


Including the Port

try {
  URL u = new URL("http",
   "www.poly.edu", 8000, "/fall97/grad.html#cs");
   // work with the URL
}
catch (MalformedURLException e) {}

Relative URLs


Constructing Relative URLs


Parsing URLs


For example,

try {
  URL u = new URL("http://www.poly.edu/fall97/grad.html#cs ");
  System.out.println("The protocol is " + u.getProtocol());
  System.out.println("The host is " + u.getHost());
  System.out.println("The port is " + u.getPort());
  System.out.println("The file is " + u.getFile());
  System.out.println("The anchor is " + u.getRef());
}
catch (MalformedURLException e) { }

Parsing URLs


Missing Pieces


Reading Data from a URL


Webcat

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

public class Webcat {

  public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
      try {
        URL u = new URL(args[i]);
        InputStream in = u.openStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        String theLine;
        while ((theLine = br.readLine()) != null) {
          System.out.println(theLine);
        }
      } 
      catch (IOException e) {
        System.err.println(e);
      } 
    }
  }
}

The Bug in readLine()


Webcat

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

public class Webcat {

 public static void main(String[] args) {

   for (int i = 0; i < args.length; i++) {
     try {
       URL u = new URL(args[i]);
       InputStream in = new BufferedInputStream(u.openStream());
       InputStreamReader isr = new InputStreamReader(in, "8859_1");
       
       char c;
       while ((c = isr.read()) != -1) {
        System.out.write(c);
       }

    } 
    catch (IOException e) {
      System.err.println(e);
    } 
 }

 }

}

CGI


Normal web surfing uses these two steps:


Forms


CGI


GET and POST


HTTP


A Typical HTTP Connection

  1. Client opens a socket to port 80 on the server.

  2. Client sends a GET request including the name and path of the file it wants and the version of the HTTP protocol it supports.

  3. The client sends a MIME header.

  4. The client sends a blank line.

  5. The server sends a MIME header

  6. The server sends the data in the file.

  7. The server closes the connection.


What the client sends to the server

GET /javafaq/images/cup.gif
Connection: Keep-Alive
User-Agent: Mozilla/3.01 (Macintosh; I; PPC)
Host: www.oreilly.com:80
Accept: image/gif, image/x-xbitmap, image/jpeg, */*

MIME


Browser Request MIME Header

Connection: Keep-Alive
User-Agent: Mozilla/3.01 (Macintosh; I; PPC)
Host: www.digitalthink.com:80
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

Server Response MIME Header


Query Strings


URL Encoding


For example,


The URLEncoder class


For example,

String qs = "Author=Sadie, Julie&Title=Women Composers";
String eqs = URLEncoder.encode(qs);
System.out.println(eqs);
  • This prints:

  • Author%3dSadie%2c+Julie%26Title%3dWomen+Composers
    

    Example

    String eqs = "Author=" + URLEncoder.encode("Sadie,
    Julie");
    eqs += "&";
    eqs += "Title=";
    eqs += URLEncoder.encode("Women Composers");
    

    The URLDecoder class


    GET URLs

    String eqs =  "Author=" + URLEncoder.encode("Sadie, Julie");
     eqs += "&";
     eqs += "Title=";
     eqs += URLEncoder.encode("Women Composers");
     try {
        URL u = new URL("http://www.superbooks.com/search.cgi?"  + eqs);
        InputStream in = u.openStream();
        //...
     }
     catch (IOException e) { //...
    

    URLConnections


    URLConnections vs. URLs


    URLConnection six steps:

    1. The URL is constructed.

    2. The URL's openConnection() method creates the URLConnection object.

    3. The parameters for the connection and the request properties that the client sends to the server are set up.

    4. The connect() method makes the connection to the server. (optional)

    5. The response header information is read using getHeaderField().

    6. The data is read from an InputStream


    I/O Across a URLConnection


    For example,

    try { 
      URL u = new URL("http://www.sdexpo.com/");
      URLConnection uc = u.openConnection();
      uc.connect(); 
      InputStream in = uc.getInputStream();
      // read the data...
    }
    catch (IOException e) { //...
    

    Reading Header Data


    getHeaderFieldKey()


    For example

    String key = null;
    for (int i=1; (key = uc.getHeaderFieldKey(i))!=null); i++) {
     System.out.println(key + ": " + uc.getHeaderField(key));
    }
    

    getHeaderFieldInt() and getHeaderFieldDate()


    Example


    Six Convenience Methods


    Example

    try {
      URL u = new URL("http://www.sdexpo.com/");
      URLConnection uc = u.openConnection();
      uc.connect();
      String key=null;
      for (int n = 1; 
           key=uc.getHeaderFieldKey(n)) != null; 
           n++) {
        System.out.println(key + ": " + uc.getHeaderField(key));
      }
    }
    catch (IOException e) {
     System.err.println(e);
    }
    

    Writing data to a URLConnection


    Eight Steps:

    1. Construct the URL.

    2. Call the URL's openConnection() method to create the URLConnection object.

    3. Pass true to the URLConnection's setDoOutput() method

    4. Create the data you want to send, preferably as a byte array.

    5. Call getOutputStream() to get an output stream object.

    6. Write the byte array calculated in step 5 onto the stream.

    7. Close the output stream.

    8. Call getInputStream() to get an input stream object. Read from it as usual.


    POST CGIs


    A POST request includes


    Example

    URLConnection uc = u.openConnection();
    uc.setDoOutput(true);
    uc.setDoInput(true);
    

    Details


    HttpURLConnection


    Recall


    Response Codes


    HTTP Protocols


    getRequestMethod()


    disconnect()


    For example,

    try {
      URL u = new URL("http://www.amnesty.org/");
      HttpURLConnection huc = (HttpURLConnection) u.openConnection();
      huc.setRequestMethod("PUT");
      huc.connect();
      OutputStream os = huc.getOutputStream();
      int code = huc.getResponseCode();
      if (code >= 200 && port < 300) { 
       // put the data...
      }
      huc.disconnect();
    }
    catch (IOException e) { //...
    

    usingProxy


    Redirect Instructions


    Example

    
    GET /~elharo/macfaq/index.html HTTP/1.0
    HTTP/1.1 302 Moved Temporarily
    Date: Mon, 04 Aug 1997 14:21:27 GMT
    Server: Apache/1.2b7
    Location: http://www.macfaq.com/macfaq/index.html
    Connection: close
    Content-type: text/html
    
    <HTML><HEAD>
    <TITLE>302 Moved Temporarily</TITLE>
    </HEAD><BODY>
    <H1>Moved Temporarily</H1>
    The document has moved
    <A HREF="http://www.macfaq.com/macfaq/index.html">here</A>.<P>
    </BODY></HTML>
    

    Example


    Following Redirects


    To Learn More

    Java Network Programming cover

    Questions?


    Index | Cafe con Leche

    Copyright 2000 Elliotte Rusty Harold
    elharo@metalab.unc.edu
    Last Modified November 8, 2000