title: Getting java.io.EOFException using HttpUrlConnection
body: <p>I am getting <code>EOFException</code> when I call a REST API. I know it says that response is null. But it should not. I use same API in an iOS application without any problem. </p>
  
  <p><strong>Here is my code :</strong> </p>
  
  <pre><code>try {
      url = new URL(baseUrl);
  }
      // Thrown when URL could not be parsed
      catch (MalformedURLException me) {
          Log.e(TAG, "URL could not be parsed. URL : " + baseUrl, me);
      }
      try {
  
      //      System.setProperty("http.keepAlive", "false");
  
      // Set connection properties
      urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod(method);
      urlConnection.setConnectTimeout(TIMEOUT * 1000);
      urlConnection.setChunkedStreamingMode(0);
      urlConnection.setRequestProperty("Accept", "*/*");
      urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
  
      //      urlConnection.setRequestProperty("Connection", "close");
  
      if (method.equals("POST") || method.equals("PUT")) {
          // Set to true when posting data
          urlConnection.setDoOutput(true);
  
          // Write data to post to connection output stream
          OutputStream out = urlConnection.getOutputStream();
          out.write(postParameters.getBytes("UTF-8"));
          Log.d(TAG, "Data written to output stream.");
      }
  
      //      urlConnection.connect();
  
      try {
          // Get response
          in = new BufferedInputStream(urlConnection.getInputStream());
      } catch (IOException e) {
              Log.e(TAG,
                      "Exception in getting connection input stream. Input stream : "
                                  + in, e);
      }
  
      Log.d(TAG, "content length : " + urlConnection.getContentLength());
      Log.d(TAG, "content type : " + urlConnection.getContentType());
  
      // Read the input stream that has response
      statusCode = urlConnection.getResponseCode();
      Log.d(TAG, "Status code : " + statusCode);
  
      if (statusCode &gt;= 400) {
          Log.e(TAG, "Error stream : " + urlConnection.getErrorStream().toString());
      }
      // Passing input stream to a function.          
      readStream(in, statusCode);
  } catch (ProtocolException pe) {
      Log.e(TAG,
                      "Make sure HTTP method is set before connecting to URL. Line : "
                              + getLineNumber(), pe);
  } catch (IllegalStateException ie) {
      Log.e(TAG,
                      "Set connection properties before connecting to URL. Line : "
                              + getLineNumber(), ie);
  }
  // Thrown when connecting to URL times out
  catch (SocketTimeoutException se) {
      Log.e(TAG, "Timeout before connecting to URL : " + baseUrl
                      + ". Line : " + getLineNumber(), se);
  
  
  } catch (IOException e) {
      Log.e(TAG, "Exception while connecting to URL : " + baseUrl, e);
  } finally {
      urlConnection.disconnect();
  }
  </code></pre>
  
  <p><strong>I have tried following things, but did not work. Theses are commented out in code. : <br></strong><br>
  1) <code>System.setProperty("http.keepAlive", "false");</code><br>
  2) <code>urlConnection.setRequestProperty("Connection", "close");</code><br>
  3) <code>urlConnection.connect();</code>  <br></p>
  
  <p>The statement <code>Log.d(TAG, "Status code : " + statusCode);</code> is not getting logged. Normally it works.</p>
  
  <p><strong>Logcat screen shot :</strong>    <br></p>
  
  <p><img src="https://i.stack.imgur.com/RItaj.png" alt="enter image description here"></p>