JMU
HTTP for Text Transport
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


A Review of HTTP
The Process
  1. The client opens a connection
  2. The client sends a request (GET, HEAD, or POST)
  3. The client waits for a response
  4. The server processes the request
  5. The server sends a response
  6. The connection is closed
Handling Special Characters
An Example

A Text File Retriever

javaexamples/internet/HTTPFileRetriever.java
        package internet;

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

/**
 * A class that retrieves text files over the Internet
 * using HTTP.
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class HTTPFileRetriever
{
    private List<String>    lines;

    /**
     * Explicit Value Constructor
     *
     * @param url  The URL of the file (including protocol)
     */
    public HTTPFileRetriever(String url)
    {
        BufferedReader  in;
        String          line;
        URL             u;

        try 
        {
            u = new URL(url);

            in = new BufferedReader(
                     new InputStreamReader(
                         u.openStream()));
            
            lines = new LinkedList<String>();
            while ( (line=in.readLine()) != null) 
            {
                try 
                {
                    lines.add(URLDecoder.decode(line, "UTF-8"));
                }
                catch (Exception de) 
                {
                    // Ignore it
                }
            }

            in.close();
        }
        catch (MalformedURLException mue)
        {
            // Do nothing
            System.err.println(mue);
        }
        catch (IOException ioe) 
        {
            // Do nothing
            System.err.println(ioe);
        }
    }

    /**
     * Get the lines in the file.
     *
     * @return  The linesi in the file
     */
    public List<String> get()
    {
        return lines;
    }
}
        
What Is In The Stream?
Processing the Entire HTTP Response
An Example

A Header Retriever/Presenter

javaexamples/internet/DisplayHeaders.java
        import java.io.*;
import java.net.*;
import java.util.*;

/**
 * An application that displays the headers in an
 * HTTP response.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DisplayHeaders
{
    /**
     * The entry point of the application.
     *
     * @param args  The command line arguments (args[0] is the URL)
     */
    public static void main(String[] args)
    {
        Map<String, List<String>> headers;        
        Set<String>               names;        
        String                    arg;
        URL                       url;
        URLConnection             connection;
        
        
        if ((args != null) && (args.length > 0)) arg = args[0];
        else arg = "https://w3.cs.jmu.edu/bernstdh/web/cs462/index.php";
        
        try
        {
            url = new URL(arg);
            connection = url.openConnection();
            connection.connect();

            headers = connection.getHeaderFields();
            names = headers.keySet();
            for (String name: names)
            {
                System.out.println(name + ":");
                List<String> values = headers.get(name);
                for (String value: values)
                {
                    System.out.println("\t"+value);
                }
            }
        }
        catch (MalformedURLException mue)
        {
            System.out.println("Invalid URL: " + arg);
            
        }
        catch (IOException ioe)
        {
            System.out.println("Unable to connect.");            
        }
    }
}