20

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("http://127.0.0.1");
URLConnection conn=url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);

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" />
</form>
mkobit
  • 43,979
  • 12
  • 156
  • 150
Borut Flis
  • 15,715
  • 30
  • 92
  • 119

4 Answers4

28

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();
    }
  }
}
Satya
  • 8,146
  • 9
  • 38
  • 43
  • I found the solution here helpful: http://stackoverflow.com/questions/36568518/testing-form-posts-through-mockmvc – Benjamin Slabbert May 07 '17 at 11:01
  • 2
    Using PostMethod is outdated for current commons (version 4), use UrlEncodedFormEntity: https://stackoverflow.com/a/8129350/1904815 – JonnyJD Oct 10 '17 at 16:10
  • 1
    @JonnyJD and from linked response, DefaultHttpClient seems to be deprecated... ;) – Line May 01 '18 at 07:01
3

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();
will
  • 23
  • 5
Aloong
  • 1,715
  • 6
  • 23
  • 40
1

It's easy to use unirest .

<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.1.02</version>
    <classifier>standalone</classifier>
</dependency>



Unirest.config().verifySsl(false);
        HttpResponse response = Unirest.post("http://127.0.0.1")
                .field("timestamp", "1571971524")
                .field("appId", "xxx")
                .field("sign", "3e345613c687110ebf437da23ad5a13f")
                .asString();
ToBeBetter
  • 11
  • 2
  • From Review:  Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Oct 25 '19 at 08:10
1

You may want to consider using HttpClient library from Apache. It's got HttpPost class, which is very easy to use.

Aleks G
  • 56,435
  • 29
  • 168
  • 265