title: HttpURLConnection getInputStream to read page, and then make POST request
body: <p>I trying to get page (HTML), read special token from it (it's different each time), and then send POST request, but that give me <code>java.lang.IllegalStateException: connect in progress</code></p>
  
  <pre><code>    final URL url = new URL("&lt;...&gt;");
      String token = null;
      final HttpURLConnection con = (HttpURLConnection) url.openConnection();
      try (final InputStreamReader isr = new InputStreamReader(con.getInputStream())) {
          try (final BufferedReader in = new BufferedReader(isr)) {
              String str;
              while ((str = in.readLine()) != null) {
                  if (str.contains("__token")) {
                      token = str.replace("&lt;...&gt;", "").replace("&lt;...&gt;", "");
                      System.out.println(token);
                      break;
                  }
              }
              in.close();
          }
          isr.close();
      }
      con.setRequestMethod("POST");
      con.setRequestProperty("User-Agent", USER_AGENT);
      con.setDoOutput(true);
      try (final DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
          final String params = getParams("par1", "par2", token);
          wr.writeBytes(params);
          wr.flush(); wr.close();
          final int responseCode = con.getResponseCode();
          System.out.println("\nSending 'POST' request to URL : " + url);
          System.out.println("Post parameters : " + params);
          System.out.println("Response Code : " + responseCode);
          try (final BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
              String inputLine;
              final StringBuilder response = new StringBuilder();
              while ((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
              }
              in.close();
              System.out.println(response.toString());
          }
      }
  </code></pre>
  
  <p>And this same if I try get this token from cookie:</p>
  
  <p><code>final String token=con.getHeaderField("Set-Cookie").replace("__Token=", "").replace("; path=/; HttpOnly", "");</code></p>
  
  <p>How to do that properly?</p>