Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 java.lang.IllegalStateException: connect in progress

    final URL url = new URL("<...>");
    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("<...>", "").replace("<...>", "");
                    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());

And this same if I try get this token from cookie:

final String token=con.getHeaderField("Set-Cookie").replace("__Token=", "").replace("; path=/; HttpOnly", "");

How to do that properly?

Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances.

This means that you cannot create a new (POST) request on the same HttpURLConnection object. And indeed, you get an IllegalStateException from it.

So you should remove the final modifier from the definition of con, and then, after finishing with the processing of the output of the original (GET) request, create another connection:

con = (HttpURLConnection) url.openConnection();

Only then, you can continue with constructing your POST request.

A little note: you use try-with-resources, so you don't need to explicitly close your streams. Also, you don't need two try blocks. In fact, you don't need to keep a reference to the InputStreamReader, as BufferedReader.close() also closes the underlying Reader, which also closes the underlying InputStream.

ok, thanks, about note: I added that code to be sure that this error isn't about open streams. – GotoFinal Dec 19, 2014 at 19:08

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.