How to send post form with java?

Apache's HttpClient project will handle this better for you.

or you can try this code:

// Using java.net.URL and  
    //java.net.URLConnection  
    URL url = new URL("http://jobsearch.dice.com/jobsearch/jobsearch.cgi");   
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);  
    OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(), "8859_1");   
    out.write("username=bob&password="+password+"");   
    // remember to clean up   
    out.flush();   
    out.close();

You can write code similar to this :

 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.http.impl.client.HttpClients;

public class PostReqEx {

  public void sendReq(String url,String email,String fname){
    HttpClient httpClient = HttpClients.createDefault();
    PostMethod postMethod = new PostMethod(url);
    postMethod.addParameter("Email", email);
    postMethod.addParameter("fname", fname);
    try {
        httpClient.executeMethod(postMethod);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
        String resp = postMethod.getResponseBodyAsString();
    } else {
         //...postMethod.getStatusLine();
    }
  }
}

Tags:

Html

Java

Post