title: Why do you have to call URLConnection#getInputStream to be able to write out to URLConnection#getOutputStream?
body: <p>I'm trying to write out to <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#getOutputStream%28%29" rel="noreferrer"><code>URLConnection#getOutputStream</code></a>, however, no data is actually sent until I call <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#getInputStream%28%29" rel="noreferrer"><code>URLConnection#getInputStream</code></a>. Even if I set <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html#doInput" rel="noreferrer"><code>URLConnnection#doInput</code></a> to false, it still will not send. Does anyone know why this is?  There's nothing in the API documentation that describes this.</p>
  
  <p>Java API Documentation on URLConnection: <a href="http://download.oracle.com/javase/6/docs/api/java/net/URLConnection.html" rel="noreferrer">http://download.oracle.com/javase/6/docs/api/java/net/URLConnection.html</a></p>
  
  <p>Java's Tutorial on Reading from and Writing to a URLConnection: <a href="http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html" rel="noreferrer">http://download.oracle.com/javase/tutorial/networking/urls/readingWriting.html</a></p>
  
  <pre><code>import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.net.URL;
  import java.net.URLConnection;
  
  public class UrlConnectionTest {
  
      private static final String TEST_URL = "http://localhost:3000/test/hitme";
  
      public static void main(String[] args) throws IOException  {
  
          URLConnection urlCon = null;
          URL url = null;
          OutputStreamWriter osw = null;
  
          try {
              url = new URL(TEST_URL);
              urlCon = url.openConnection();
              urlCon.setDoOutput(true);
              urlCon.setRequestProperty("Content-Type", "text/plain");            
  
              ////////////////////////////////////////
              // SETTING THIS TO FALSE DOES NOTHING //
              ////////////////////////////////////////
              // urlCon.setDoInput(false);
  
              osw = new OutputStreamWriter(urlCon.getOutputStream());
              osw.write("HELLO WORLD");
              osw.flush();
  
              /////////////////////////////////////////////////
              // MUST CALL THIS OTHERWISE WILL NOT WRITE OUT //
              /////////////////////////////////////////////////
              urlCon.getInputStream();
  
              /////////////////////////////////////////////////////////////////////////////////////////////////////////
              // If getInputStream is called while doInput=false, the following exception is thrown:                 //
              // java.net.ProtocolException: Cannot read from URLConnection if doInput=false (call setDoInput(true)) //
              /////////////////////////////////////////////////////////////////////////////////////////////////////////
  
          } catch (Exception e) {
              e.printStackTrace();                
          } finally {
              if (osw != null) {
                  osw.close();
              }
          }
  
      }
  
  }
  </code></pre>