1

I use the code

public static int getResponseCode(String urlString) throws MalformedURLException, IOException {         
    URL u = new URL(urlString);  
    HttpURLConnection huc = (HttpURLConnection) u.openConnection();
    huc.setRequestMethod("GET");
    huc.connect();
    return huc.getResponseCode();  
}  

to get the Response code. When I run the code, I get an exception java.net.ConnectException: Connection timed out at java.net.PlainSocketImpl.socketConnect(Native Method)

I printed the URl which makes this error and then loaded it in browser. the link was loaded, but after some time, around 2 mins. What could be the reason why the status code is not returned? And How can I handle this case?

Thanks for your help!

Shari

Shari
  • 123
  • 1
  • 1
  • 9

2 Answers2

0

Try to call huc.setReadTimeout(120000); before huc.connect().

However I am afraid that something is going wrong with your server side. Why does it replies after ~2 minutes?

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

You are saying that it took 2 minutes to load content in the browser. Most likely, you connection times out. You can try to set a longer timeout for your connection to 3 minutes in milliseconds

huc.setConnectTimeout(1800000);

If the issue is not timeout, then most likely you have to set the proxy for your connection. Check the proxy settings in your browser and set the appropriate values. See, for example, this question, on how to do it.

Community
  • 1
  • 1
jny
  • 8,007
  • 3
  • 37
  • 56