title: Why the code throws "Cannot write output after reading input" error?
body: <p>Please check the code below. What the code does is use <code>firstHttpConn</code> to check the response code and open <code>secondHttpConn</code>. If the response code is 401, add a basic auth header to <code>secondHttpConn</code>. Then post the data and read the response. </p>
  
  <p>However, the code throws <code>Cannot write output after reading input</code> error. I checked other questions on Stack Overflow like <a href="https://stackoverflow.com/questions/11413028/cannot-write-output-after-reading-input">this one</a> and I am sure I did not make the same mistakes.</p>
  
  <pre><code>//I hate using Java 6 and HttpURLConnection, but the code is for a very old system.
  private static XmlObject callWebService(String soapMessage, String webServiceEndpoint, String soapAction) throws Exception {
      XmlObject resultXMLObject;
      HttpURLConnection firstHttpConn = null;
      HttpURLConnection secondHttpConn = null;
  
      try {
          byte[] streamoutByteArray = soapMessage.getBytes("UTF-8");
  
          URL url = new URL(webServiceEndpoint);
          firstHttpConn = (HttpURLConnection) url.openConnection();
          firstHttpConn.connect();
  
          if (firstHttpConn.getResponseCode() == 401) {
              firstHttpConn.disconnect();
              secondHttpConn = (HttpURLConnection) url.openConnection();
              secondHttpConn.setRequestProperty("Authorization", "Basic " + getBasicAuth());
          } else {
              firstHttpConn.disconnect();
              secondHttpConn = (HttpURLConnection) url.openConnection();
          }
  
          secondHttpConn.setConnectTimeout(30000);
          secondHttpConn.setReadTimeout(300000);
          secondHttpConn.setRequestProperty("Content-Length", String.valueOf(streamoutByteArray.length));
          secondHttpConn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8);
          secondHttpConn.setRequestProperty("SOAPAction", soapAction);
          secondHttpConn.setRequestMethod("POST");
          secondHttpConn.setDoOutput(true);
          secondHttpConn.setDoInput(true);
  
          OutputStream out = secondHttpConn.getOutputStream();
          out.write(streamoutByteArray);
          out.close();
  
          //Line 238 below.
          InputStreamReader isr = new InputStreamReader(secondHttpConn.getInputStream());
          BufferedReader in = new BufferedReader(isr);
          String inputLine;
          StringBuffer sb = new StringBuffer();
  
          while ((inputLine = in.readLine()) != null)
              sb.append(inputLine).append("\n");
  
          in.close();
          resultXMLObject = XmlObject.Factory.parse(sb.toString());
      } finally {
          if (firstHttpConn != null)
              firstHttpConn.disconnect();
          if (secondHttpConn != null)
              secondHttpConn.disconnect();
      }
  
      return resultXMLObject;
  }
  </code></pre>
  
  <p>The log:</p>
  
  <pre><code>Caused By: java.net.ProtocolException: Cannot write output after reading input.
          at weblogic.net.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:271)
          at org.company.member.esb.webservice.WebServiceClient.callWebService(WebServiceClient.java:238)
          at org.company.member.esb.webservice.WebServiceClient.invokeWebserviceV01(WebServiceClient.java:132)
          at sun.reflect.GeneratedMethodAccessor711.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          Truncated. see log file for complete stacktrace
  </code></pre>
  
  <p>Anyone knows why? Thanks.</p>