FileDocCategorySizeDatePackage
FormPoster.javaAPI DocExample2490Sat Sep 09 20:52:40 BST 2000None

FormPoster.java

import java.net.*;
import java.io.*;
import com.macfaq.net.*;


public class FormPoster {

  private URL url;
  // from Chapter 7, Example 7-9
  private QueryString query = new QueryString(); 
   
  public FormPoster (URL url) throws IllegalArgumentException {
    if (!url.getProtocol().toLowerCase().startsWith("http")) {
      throw new IllegalArgumentException("Posting only works for http URLs");   
    }    
    this.url = url;
  }
  
  public void add(String name, String value) {
    query.add(name, value);    
  }
  
  public URL getURL() {
    return this.url; 
  }

  public InputStream post() throws IOException {

    // open the connection and prepare it to POST
    URLConnection uc = url.openConnection();
    System.out.println("do output: " + uc.getDoOutput());
    System.out.println("do input: " + uc.getDoInput());
    uc.setDoOutput(true);
    System.out.println("do output: " + uc.getDoOutput());
    System.out.println("do input: " + uc.getDoInput());
    OutputStreamWriter out = new OutputStreamWriter(
     new BufferedOutputStream(uc.getOutputStream()), "ASCII");
    InputStream in = uc.getInputStream();

    // The POST line, the Content-type header,
    // and the Content-length headers are sent by the URLConnection.
    // We just need to send the data
    out.write(query.toString());
    out.write("\r\n");
    out.flush();
    out.close();


    // Return the response
     return in;

  }


  public static void main(String args[]) {

    URL url;

    if (args.length > 0) {
      try {
        url = new URL(args[0]);
      }
      catch (MalformedURLException e) {
        System.err.println("Usage: java FormPoster url");
        return;
      }
    }
    else {
      try {
        url = new URL("http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query");
      }
      catch (MalformedURLException e) { // shouldn't happen
        System.err.println(e);
        return; 
      }
    }

    FormPoster poster = new FormPoster(url);
    poster.add("name", "Elliotte Rusty Harold");
    poster.add("email", "elharo@metalab.unc.edu");
    
    try {
      InputStream in = poster.post();
      in = new BufferedInputStream(in);
      // Read the response
      InputStreamReader r = new InputStreamReader(in);
      int c;
      while((c = r.read()) != -1) {
        System.out.print((char) c);
      }
      System.out.println();
      in.close();
    }
    catch (IOException e) {
      System.err.println(e);
      e.printStackTrace();   
    }

  }

}