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 ?

1

3 Answers 3

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);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for answering the question instead of jumping to an alternative library.
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.