1

i'm looking for tutorial or quick example, how i can send POST data throw openStream.

My code is:

URL url = new URL("http://localhost:8080/test");
            InputStream response = url.openStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));

Could you help me ?

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
Ilkar
  • 2,113
  • 9
  • 45
  • 71
  • possible duplicate of [Sending HTTP POST Request In Java](http://stackoverflow.com/questions/3324717/sending-http-post-request-in-java) – Brian Roach Jan 10 '12 at 14:10

3 Answers3

8
    URL url = new URL(urlSpec);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(method);
    connection.setDoOutput(true);
    connection.setDoInput(true);

    // important: get output stream before input stream
    OutputStream out = connection.getOutputStream();
    out.write(content);
    out.close();        

            // now you can get input stream and read.
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        writer.println(line);
    }
Gomino
  • 12,127
  • 4
  • 40
  • 49
AlexR
  • 114,158
  • 16
  • 130
  • 208
1

Use Apache HTTP Compoennts http://hc.apache.org/httpcomponents-client-ga/

tutorial: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html

Look for HttpPost - there are some examples of sending dynamic data, text, files and form data.

Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
0

Apache HTTP Components in particular, the Client would be the best way to go. It absracts a lot of that nasty coding you would normally have to do by hand

jere
  • 4,306
  • 2
  • 27
  • 38