Borut Flis Borut Flis - 2 months ago 12
HTML Question

How to send post form with java?

I would like to send a post form with java on a website. I came up with this, but I dont what to do next or if this is even the right way.

URL url = new URL("");
URLConnection conn=url.openConnection();
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

the post form looks like this.

<form action="prikaz4.php" method="post">
<select name="igralec"/>
<option value="Kobe Bryant">Kobe Bryant</option>
<option value="Dwayne Wade">Dwayne Wade</option>
<input type="submit" />


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 {
    } catch (HttpException e) {
    } catch (IOException e) {

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