FileDocCategorySizeDatePackage
FormPoster.javaAPI DocExample2145Sun Dec 12 10:55:12 GMT 2004None

FormPoster

public class FormPoster extends Object

Fields Summary
private URL
url
private QueryString
query
Constructors Summary
public FormPoster(URL url)

 
   
      
    if (!url.getProtocol().toLowerCase().startsWith("http")) {
      throw new IllegalArgumentException(
       "Posting only works for http URLs");   
    }    
    this.url = url;
  
Methods Summary
public voidadd(java.lang.String name, java.lang.String value)

    query.add(name, value);    
  
public java.net.URLgetURL()

    return this.url; 
  
public static voidmain(java.lang.String[] args)


    URL url;

    if (args.length > 0) {
      try {
        url = new URL(args[0]);
      }
      catch (MalformedURLException ex) {
        System.err.println("Usage: java FormPoster url");
        return;
      }
    }
    else {
      try {
        url = new URL(
          "http://www.cafeaulait.org/books/jnp3/postquery.phtml");
      }
      catch (MalformedURLException ex) { // shouldn't happen
        System.err.println(ex);
        return; 
      }
    }

    FormPoster poster = new FormPoster(url);
    poster.add("name", "Elliotte Rusty Harold");
    poster.add("email", "elharo@metalab.unc.edu");
    
    try {
      InputStream in = poster.post();
    
      // 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 ex) {
      System.err.println(ex);   
    }

  
public java.io.InputStreampost()


    // open the connection and prepare it to POST
    URLConnection uc = url.openConnection();
    uc.setDoOutput(true);
    OutputStreamWriter out 
     = new OutputStreamWriter(uc.getOutputStream(), "ASCII");

    // 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 uc.getInputStream();