title: "java.lang.IllegalStateException: Cannot set request property after connection is made" error in Android
body: <p>I am new to Android and trying to get HTML content of a web page and that is when I came across this issue.</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot set request property after connection is made
  </code></pre>
  
  <p><strong>Code :</strong></p>
  
  <pre><code>public static String getHTMLPage(String inputURL, String host, String referer, String cookie, String breakString) {
  
      String htmlContent = "";
      try {
          URL u = new URL(inputURL);
          HttpURLConnection uc = (HttpURLConnection) u.openConnection();
          uc.setRequestProperty("Host", host);
          uc.setRequestProperty("Connection", "keep-alive");
          if (!referer.isEmpty()) {
              uc.setRequestProperty("Referer", referer);
          }
          uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1");
          uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
          uc.setRequestProperty("Accept-Encoding", "html");
          uc.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
          uc.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
  
          if (!cookie.isEmpty()) {
              uc.setRequestProperty("Cookie", cookie);
          }
  
          BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
          String line = "";
          while ((line = br.readLine()) != null) {
              if (!breakString.isEmpty() &amp;&amp; line.contains(breakString)) {
                  break;
              }
              htmlContent += line;
          }
          br.close();
      } catch (Exception e) {
          System.out.println(e);
      }
      return htmlContent;
  }
  </code></pre>
  
  <p>Basically, I have written this in a pure <code>java</code> application (where it is working fine) and I just wanted to re-use the same. I have googled and found few SO pages (<a href="https://stackoverflow.com/questions/19565195/android-setrequestproperty-in-a-url-openconnection">1</a>,<a href="https://stackoverflow.com/questions/22320418/setrequestproperty-throwing-java-lang-illegalstateexception-cannot-set-request">2</a>,<a href="https://stackoverflow.com/questions/29765186/exception-cannot-set-request-property-after-connection-is-made-occurs-on-andro">3</a>,<a href="https://stackoverflow.com/questions/7726195/setresponsecode-method-giving-java-lang-illegalstateexception-cannot-set-method">4</a>), but none of them addresses the problem specific to <code>HTTPURLConnection</code> directly. (Though, some advised about <code>HTTPClient</code>, which is found to be deprecated which I don't want to use.)</p>
  
  <p>Adding <code>getResponseCode()</code> is returning the value as 200 successfully.</p>
  
  <pre><code>System.out.println("RESPONSE CODE : "+uc.getResponseCode());
  </code></pre>
  
  <p>I have looked the <a href="http://developer.android.com/reference/java/net/URLConnection.html#setRequestProperty(java.lang.String,%20java.lang.String)" rel="nofollow noreferrer">reference</a> page and which states  </p>
  
  <p><strong>public void setRequestProperty (String field, String newValue)</strong></p>
  
  <blockquote>
    <p>Sets the value of the specified request header field. The value will
    only be used by the current URLConnection instance. This method can
    only be called before the connection is established.</p>
  </blockquote>
  
  <p>How I can set the request property before making the connections ? As per the code, we are getting the <code>HTTPURLConnection</code> object only after opening connection for a specific URL (and casting it with <code>HTTPURLConnection</code>)</p>
  
  <pre><code>         HttpURLConnection uc = (HttpURLConnection) u.openConnection();
  </code></pre>
  
  <p>What is the correct way to use <code>HTTPURLConnection</code> to read HTML page along with <code>setRequestProperty</code> ? </p>