1

I am very new to Java and all its glory.

Using HttpsUrlConnection, I am able to do most the REST operations without much problem. However, how do I go about creating a persistent Https connection? I need this connection for asynchronous messages from the server. Is there a simple java solution that only uses the java.net package? I am not looking for elegant solution, just something simple and work.

Here is what I have so far and can't seem to receive anything from the server. I have got a 200 status in response but nothing come in after that. I did verified that the server is in fact sending out async messages. I can receive them using a different tool. (using python request with stream )

Prior to this code, I have successfully logged in and got all the required cookies and authToken. Cookie Manager is enabled. additionalAuthToken(conn) takes care of adding the required authorization stuff. This proved to work in other REST call so I don't suspect authorization to be the problem. Again, if authorization was the problem, the server would not have return 200.

    URL obj = new URL(url);
    HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
    conn.setRequestProperty("Connection", "keep-alive"); 
    conn.setRequestMethod("GET"); 
    additionalAuthToken(conn);


    conn.setDoOutput(true);
    conn.setDoInput(true);

    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
    }

    System.out.println(url);
    System.out.println(conn.getResponseMessage());

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while( true ) {

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            System.out.println(inputLine);
        }
        Thread.sleep(100);
        System.out.println("Go around");
    }

Is this possible? if not, what are your recommendation to achieve the persistent https channel? I have heard about netty and a few other from google results. For now, I really hope for a simple java.net solution without another having another dependency.

Sincerely, Tai

1
  • Follow just in case someone else is looking for the answer. It does work. This in fact does work. The problem was i had receiving code in a seperate function which closes the stream and return. While type up the question, I move that code out here and does not close the BufferReader. So now, I am getting steady async message. So the moral of the story is not to close the stream. This code does actually work. Commented Feb 12, 2016 at 17:25

1 Answer 1

1

As far as URLConnection goes, here's all the information about how to make it persistent: Persistent Connections

If you can, I recommend you use Apache httpcomponents for which there are many examples: This one in particular shows the basics of persistent connections.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.