This is very useful at many times, if you want to emulate a post request from a browser. I use it all the time, it’s very useful, so i put it here for you to see:

 

try {
    // Construct data
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
 
    // Send data
    URL url = new URL("http://hostname:80/cgi");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();
 
    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        // Process line...
    }
    wr.close();
    rd.close();
} catch (Exception e) {
}

If you don’t know exactly what to post, you can use live http headers addon for firefox. Just install the addon, open it (tools menu), fill the form you want to emulate, then click submit on the website you want to emulate the post

 

post request with java

If you click copy this is what you get: email=contact%email.gr&password=password&login=Sign+In

This is all the post data you need. Just delete:

String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8"); data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

And put:

String data = "email=contact%email.gr&password=password&login=Sign+In";