import java.net.*;
import java.util.*;
import java.io.*;

// Make an HTTP 1.0 post
public class HTTPForm extends Properties {
    public boolean send(String page, OutputStream outstr) throws IOException {
	StringBuffer data=new StringBuffer();
	String headers;
	Enumeration i=keys();
	while (i.hasMoreElements()) {
	    String key=(String)i.nextElement();
	    data.append(URLEncoder.encode(key)
			+ "=" + 
			URLEncoder.encode((String)get(key))+
			"&");
	}
	data.deleteCharAt(data.length()-1);  // remove last &
	headers="POST " + page + " HTTP/1.0\r\n"+
	    "Content-Type: application/x-www-form-urlencoded\r\n" +
	    "Content-Length: " + data.length() + "\r\n\r\n";
        outstr.write(headers.getBytes());
	outstr.write(data.toString().getBytes());
	return true;
    }

    // Test main -- duplicates MailFind
    public static void main(String args[]) throws Exception {
        HTTPForm test=new HTTPForm();
	Socket sock=new Socket("new.usps.com",80);
	OutputStream output=sock.getOutputStream();
	InputStream input=sock.getInputStream();
	test.put("tracknbr",args[0]);
	test.send("/cgi-bin/cttgate/ontrack.cgi",output);
// For this example, just dump results
	int c;
	do {
	    c=input.read();
	    if (c!=-1) System.out.print((char)c);
	} while (c!=-1);
	sock.close();
    }
}
