HttpUrlConnection that sends multiple post request in single connection

ram picture ram · Sep 16, 2016 · Viewed 9.2k times · Source

I want to make multiple post calls in a single HTTP connection,

  1. I will be sending an Arraylist<String> and httpConnection objects as the input parameters.
  2. Iterate the ArrayList and write the request to the server.

I end up getting the below error :

Cannot write output after reading input.
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)

Here is my code. I am using to accomplish the above task.

public boolean sendToLogsAPI(ArrayList<String> logList, HttpURLConnection conn) throws IOException
{
    try 
    {
         DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
         for(int i=0; i<logList.size(); i++)
         {
             wr = new DataOutputStream(conn.getOutputStream());
             wr.writeBytes(logList.get(i));
             wr.flush();
             int nothing = conn.getResponseCode();
             String morenothing = conn.getResponseMessage();
         }   
         wr.flush();
         wr.close();
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    } 
    finally 
    {
      if(conn != null) 
      {
        conn.disconnect(); 
      }
    }

    return false;
}

How can I overcome this situation?

Answer

Balkrishna Rawool picture Balkrishna Rawool · Sep 16, 2016

According to the javadoc of HttpURLConnection:

Each HttpURLConnection instance is used to make a single request

See this for more info.

The problem is you cannot do conn.getOutputStream() once you've done wr.flush().

You have to create a new instance of HttpURLConnection if you want to send another post request. You can do it in multiple ways. One way is to create a method getHttpURLConnection() which gives new connection every time [If you show how the instance of HttpURLConnection is created which is being passed to method sendToLogsAPI(), then I can show you the implementation of getHttpURLConnection() as well.] and modify existing code as follows:

public boolean sendToLogsAPI(ArrayList<String> logList) throws IOException
{
    DataOutputStream wr = null;
    HttpURLConnection conn = null;

    try 
    {
         for(int i=0; i<logList.size(); i++)
         {
             conn = conn("<Some-URL>","<API-Key>","<GUID>");
             wr = new DataOutputStream(conn.getOutputStream());
             wr.writeBytes(logList.get(i));
             wr.flush();
             int nothing = conn.getResponseCode();
             String morenothing = conn.getResponseMessage();
         }   
         if(wr != null) {
             wr.close();
         }
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    } 
    finally 
    {
      if(conn != null) 
      {
        conn.disconnect(); 
      }
    }

    return false;
}

Another question I'd like to ask you is why do you want to use the same HttpURLConnection instance. Even if you use multiple of those, the same Socket (and underlying TCP) could be used. So don't worry about multiple instances of HttpURLConnection.