The Java Program: CGI.java

  1 import Query;
  2 import PostQuery;
  3 import GetQuery;
  4 import java.net.MalformedURLException;
  5 import java.net.UnknownHostException;
  6 import java.io.BufferedReader;
  7 import java.io.InputStreamReader;
  8 import java.io.IOException;
  9 import java.io.FileNotFoundException;
 10 
 11 
 12 public class CGI {
 13    /* accepts POST or GET with any input */
 14    static String actionURL = "http://www.cs.fit.edu/cgi-bin/cgi11-html";
 15 
 16    public static void main (String args[]) {
 17       try {
 18          Query q = new PostQuery (actionURL);
 19          q.add ("street", "150 West Univeristy");
 20          q.add ("city", "Melbourne");
 21          q.add ("zip", "32901");
 22 
 23          // Get the response, possibly an HTML file
 24          BufferedReader in =
 25             new BufferedReader (new InputStreamReader(q.send()));
 26          String line;
 27          while ((line = in.readLine()) != null) {
 28             // data from the CGI
 29             System.out.println (line);
 30          }
 31          in.close();
 32       } catch (MalformedURLException e) {
 33          e.printStackTrace();
 34       } catch (FileNotFoundException e) {
 35          System.out.println ("Does the URL exist?");
 36          e.printStackTrace();
 37       } catch (IOException e) {
 38          e.printStackTrace();
 39       }
 40       
 41    }
 42 }
 43 
 44 /*
 45   Some other URLs ...
 46   
 47   Method: Post
 48   Action URL: "http://java.sun.com/cgi-bin/backwards";
 49   <INPUT NAME="string">
 50 
 51   Method: Post
 52   Action URL: "http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query";
 53   with any input
 54 
 55 
 56   I can't get USPS to respond this way; they say they do not
 57   "batch" processing.
 58 
 59   Method: Post
 60   Action URL: http://zip4.usps.gov/cgi-bin/zip4/zip4inq
 61   Encoding: application/x-www-form-urlencoded (default)
 62 
 63   <INPUT NAME="street">
 64   <INPUT NAME="city">
 65   <INPUT NAME="zip">
 66 */