// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.*;
import java.net.*;

public class PostTest {
   private int x;
   public static StringBuffer postToURL(String postURL, String postParams) {
      StringBuffer response = new StringBuffer(300);
      if (postURL == null)
         return null;
      try {
         byte[] params = postParams.getBytes();
         URL url = new URL(postURL);
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
         con.setDoOutput(true);
         con.setDoInput(true);
         con.setRequestMethod("POST");
         con.setRequestProperty("Content-length", params.length + "");
         OutputStream out = con.getOutputStream();
         out.write(params);
         out.flush();
         BufferedReader in =
            new BufferedReader(new InputStreamReader(con.getInputStream()));
         while (true) {
            String line = in.readLine();
            if (line == null)
               break;
            response.append(line);
         }
         in.close();
         out.close();
         con.disconnect();
      } catch (Exception e) {
         response.append("Error connecting to server: " + e.getMessage());
      }
      return response;
   }
   public static void main(String[] args) {
      System.out.println(postToURL("http://www.domain.dk/htbin/tell2", "f1=val1&f2=val2"));
   }
}
