The Java Program: Query.java

  1 // Query.java -- abstract class for CGI queries
  2 
  3 import java.net.URL;
  4 import java.net.URLEncoder;
  5 import java.net.URLConnection;
  6 import java.io.IOException;
  7 import java.io.InputStream;
  8 
  9 /*
 10   The use of this class goes something like this:
 11   1)  Create an instance of this class and establish the URL.
 12   2)  Add all the name/value pairs comprising the query.
 13   3)  Issue the "send" command; it returns the response in
 14       an input stream.
 15 
 16   The two subclass "PostQuery" and "GetQuery" provided the
 17   details for CGI programs expecting the "POST" method and
 18   the "GET" method respectively.
 19 */
 20 
 21 abstract class Query {
 22 
 23    protected String url_name;
 24 
 25    protected String query = null;
 26 
 27    public void add (String name, String value) {
 28       String pair = URLEncoder.encode(name) + "=" +
 29                     URLEncoder.encode(value);
 30       
 31       if (query==null) {
 32          query = pair;
 33       } else {
 34          query += "&" + pair;
 35       }
 36    }
 37 
 38    abstract public InputStream send () throws IOException;
 39 }
 40