// 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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Upload {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://localhost/upload.ashx?file=z.txt");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        PrintStream ps = new PrintStream(con.getOutputStream());
        ps.println("123");
        ps.println("This is a test!");
        ps.flush();
        con.connect();
        if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line;
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
         } else {
             System.out.println("Status=" + con.getResponseCode());
             System.out.println("Status text=" + con.getResponseMessage());
        }
        con.disconnect();
    }
}
