title: My questions about HttpURLConnections on android. (cast-URLConnection, url.openConnection, getOutPutStream().. )
body: <p>I have a problem about creating a httpurlconnection(or httpsurlconnection). Actually this is about setting connection headers after casting url.openconnection.Before the running same code is not running now. I couldn't understand. (Exception  : already connected... or like.. )</p>
  
  <p>Some times it runs until <strong>httpConn.getOutPutStream();</strong> At this time it gives error "java.net.ProtocolException: OutputStream unavailable because request headers have already been sent!"</p>
  
  <p>Where is my fault??  </p>
  
  <p>Before I used code: All code is <a href="https://stackoverflow.com/questions/7765524/what-difference-httpconnection-on-j2me-between-httpurlconnection-on-android-htt">here</a> (I have another questıon about <a href="https://stackoverflow.com/questions/7765524/what-difference-httpconnection-on-j2me-between-httpurlconnection-on-android-htt">This Code</a>, too)</p>
  
  <pre><code>URL url = new URL(getUrl());
  URLConnection conn = url.openConnection();
  HttpURLConnection httpConn=(HttpURLConnection)conn; /*or HttpsURLConnection*/
  httpConn.set...
  ...
  httpConn.connect();
  ...
  </code></pre>
  
  <p>I try to set some settings after casting , but result is same. conn.set..'s are working, but after casting I can't set anything... </p>
  
  <p>Changed Code:</p>
  
  <pre><code>        URL url = new URL(getUrl());
          conn = url.openConnection();
          conn.setAllowUserInteraction(false);
          conn.setConnectTimeout(10000);
          conn.setRequestProperty("Accept-Charset", "utf-8");
          conn.setRequestProperty("Content-Type",
                  "text/xml; charset=utf-8");
          conn.setRequestProperty("SOAPAction",
                  "http://tempuri.org/IAuthenticationServiceNew/Authenticate");
          conn.setRequestProperty("Software-Version", AppData.VERSION);
          conn.setDoOutput(true);
          httpConn = (HttpsURLConnection) conn;
          httpConn.setChunkedStreamingMode(getParams().getBytes("UTF8").length);
          httpConn.setInstanceFollowRedirects(true);
  
          httpConn.connect();
          os = httpConn.getOutputStream();
          os.write(getParams().getBytes("UTF8"));
  </code></pre>