HttpURLConnection inside a loop

Posted by Carlos Garces on Stack Overflow See other posts from Stack Overflow or by Carlos Garces
Published on 2010-06-10T15:29:01Z Indexed on 2010/06/10 16:32 UTC
Read the original article Hit count: 358

Hi! I'm trying to connect to one URL that I know that exist but I don't know when. I don't have access to this server so I can't change anything to receive a event.

The actual code is this.

URL url = new URL(urlName);
for(int j = 0 ; j< POOLING && disconnected; j++){
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    int status = connection.getResponseCode();
    if(status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_NOT_MODIFIED){
        //Some work
    }else{
        //wait 3s
        Thread.sleep(3000);       
    }
}

Java not is my best skill and I'm not sure if this code is good from the point of view of performance. I'm opening a new connection every 3 seconds? or the connection is reused?

If I call to disconnect() I ensure that no new connections are open in the loop, but... it will impact in performance?.

Suggestions? What is the fast/best ways to know it a URL exist?

© Stack Overflow or respective owner

Related posts about java

Related posts about Performance