Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpUrlConnection request not working without reading the response

I'm trying to post a json request via HttpUrlConnection (Note that I just need to push data to the server, response will be ignored). I've run through few examples but still couldn't get it work, no data was added to the server.

Here's my code:

   URL url = new URL(urlString);
   connection = (HttpURLConnection) url.openConnection();
   connection.setRequestMethod("POST");
   connection.setDoOutput(true);
   connection.setRequestProperty("Content-Type", "application/json");
   connection.setRequestProperty("Accept", "application/json");

   OutputStream stream = connection.getOutputStream();
   stream.write(requestJSON.toString().getBytes());
   stream.flush();
   stream.close();

Update

I noticed that the code works when I try to read the response after stream.close(), eg:

connection.getResponseMessage()

Is that means it's necessary to read the response in order to complete the request?

like image 521
Mark KWH Avatar asked Dec 29 '25 21:12

Mark KWH


1 Answers

use this

if request is GET

 public static String getDataUsingGetRequest(String urlVal) {
    String Content = null;
    BufferedReader reader = null;
    try {
        URL url = new URL(urlVal);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setConnectTimeout(15 * 1000);
        reader = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }

        Content = sb.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (Exception ex) {
        }
    }
    return Content;
}

// if request is post

public static String getDataUsingPostRequest(String urlStr,  String inputJson) {

    String result = null;
    try{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Content-type", "application/json");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.connect();
        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(inputJson);
        writer.flush();
        writer.close();
        os.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        result = sb.toString();
    }catch(Exception e){
        e.printStackTrace();
    }
    return result;
}
like image 127
Ashish Agrawal Avatar answered Jan 01 '26 12:01

Ashish Agrawal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!