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