0

I am using HttpURLConnection to make GET requests. Below is the code:

class ConnectClass  {
        private HttpURLConnection connection;

        public makerequest(URL url)  throws Exception {
        try {
            connection = (HttpsURLConnection) url.openConnection();
            ..... 
            } finally {
             connection.disconnect();
            }
        }
}

Is it a good idea to create connection and close it everytime for each request? How can I improve latency? Is it possible to reuse the existing connection without creating a new one everytime.

Any ideas? Thanks in advance.

ok2c
  • 26,450
  • 5
  • 63
  • 71
Mounika
  • 11
  • 1
  • I recommend for you to search with terms "java keep-alive" – MNEMO Jul 28 '23 at 04:38
  • You don't have any choice. There is no other way to use `Http[s]URLConnection`. The good news is that it does connection pooling itself behind the scenes. But don't call `disconnect()`. Just close the input stream. – user207421 Jul 28 '23 at 06:36

0 Answers0