Skip to content Server returned HTTP response code: 411 in JAVA

I’m checking whether internet is available or not

URL url = new URL("");
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // set connect timeout.

            // set read timeout.





            Integer code = conn.getResponseCode();
            final String contentType = conn.getContentType();

While running this code i’m getting the exception Server returned HTTP response code: 411

What could be the error.


HTTP status code 411 means “length required” – you’ve tried to make a POST request, but you’ve never provided any input data. The Java client code isn’t setting a Content-Length header, and the server is rejecting a POST request with no length.

Why are you even trying to make a post at all? Why not make a GET request, or better yet a HEAD?

I’d also recommend that if you really need to know whether some specific site is up (e.g. a web service) that you connect to that, rather than just to Google.