#### POST Operation

The standard HTTP request submitted by the parser is a GET. This note describes how to use POST, which is the usual request submitted by a form.

As an example, we'll submit a form to the U.S. postal service web site.
Note: This is suboptimal, the postal service provides tools for this type of thing: http://www.uspswebtools.com

On the USPS web site, the page http://www.usps.com/zip4/citytown.htm has the following FORM that asks for a zip code and returns the cities or towns covered by the zip code (only form elements are shown removing all the formatting markup):

    <form NAME="frmzip" ACTION="zip_response.jsp" METHOD="post" OnSubmit="return validate(frmzip)">
    <input type="text" id="zipcode" name="zipcode" size="5" maxlength="5" TABINDEX="10">
    <input TYPE="image" NAME="Submit" SRC="/zip4/images/submit.jpg" BORDER="0" WIDTH="50" HEIGHT="17" ALT="Submit" TABINDEX="11">

    From this we determine that the METHOD is POST and the form should be submitted to zip_response.jsp. This relative URL is relative to the page it is found on, so the form should be submitted to http://www.usps.com/zip4/zip_response.jsp when the Submit input is clicked. The only input element other than the Submit is a single text field that takes 5 or fewer characters. Other types of input element are described in http://www.w3.org/TR/html4/interact/forms.html.

    The basic operation is to pass a fully prepared HttpURLConnection connected to the POST target URL into the Parser, either in the constructor or via the setConnection() method. To condition the connection, use the setRequestMethod() method to set the POST operation, and the setRequestProperty() and other explicit method calls. Then write the input fields as an ampersand concatenation ("input1=value1&input2=value2&...") into the PrintWriter obtained by a call to getOutputStream().

    The following sample program illustrates the principles using a StringBean, but the same code could be used with a Parser by replacing the last three lines in the try block with:

            parser = new Parser ();
            parser.setConnection (connection);
            // ... do parser operations

    Source Code.Source Code. Pretty Print Source CodePretty Print Source Code

    /*
     * Zip.java
     * POST zip code to look up cities.
     *
     * Created on April 20, 2003, 11:09 PM
     */

    import java.io.PrintWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;

    import org.htmlparser.beans.StringBean;

    /**
     * POST zip code to look up cities.
     * @author Derrick Oswald
     */
    public class Zip
    {
        String mText; // text extracted from the response to the POST request

        /**
         * Creates a new instance of Zip
         */
        public Zip (String code)
        {
            URL url;
            HttpURLConnection connection;
            StringBuffer buffer;
            PrintWriter out;
            StringBean bean;

            try
            {
                // from the 'action' (relative to the refering page)
                url = new URL ("http://www.usps.com/zip4/zip_response.jsp");
                connection = (HttpURLConnection)url.openConnection ();
                connection.setRequestMethod ("POST");

                connection.setDoOutput (true);
                connection.setDoInput (true);
                connection.setUseCaches (false);

                // more or less of these may be required
                // see Request Header Definitions: http://www.ietf.org/rfc/rfc2616.txt
                connection.setRequestProperty ("Accept-Charset", "*");
                connection.setRequestProperty ("Referer", "http://www.usps.com/zip4/citytown.htm");
                connection.setRequestProperty ("User-Agent", "Zip.java/1.0");

                buffer = new StringBuffer (1024);
                // 'input' fields separated by ampersands (&)
                buffer.append ("zipcode=");
                buffer.append (code);
                // buffer.append ("&");
                // etc.

                out = new PrintWriter (connection.getOutputStream ());
                out.print (buffer);
                out.close ();

                bean = new StringBean ();
                bean.setConnection (connection);
                mText = bean.getStrings ();
            }
            catch (Exception e)
            {
                mText = e.getMessage ();
            }

        }

        public String getText ()
        {
            return (mText);
        }

        /**
         * Program mainline.
         * @param args The zip code to look up.
         */
        public static void main (String[] args)
        {
            if (0 >= args.length)
                System.out.println ("Usage:  java Zip <zipcode>");
            else
                System.out.println (new Zip (args[0]).getText ());
        }
    }

    Last edited on Thursday, April 24, 2003  4:36:36 am.