Skip to content

URLjava.io.IOException: Server returned HTTP response code: 411 in JAVA

I’m checking whether internet is available or not

URL url = new URL("http://www.google.co.in/");
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // set connect timeout.
            conn.setConnectTimeout(1000000);

            // set read timeout.
            conn.setReadTimeout(1000000);

            conn.setRequestMethod("POST");

            conn.setRequestProperty("Content-Type","text/xml");

            conn.setDoOutput(true);

            conn.connect();

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

While running this code i’m getting the exception

URLjava.io.IOException: Server returned HTTP response code: 411

What could be the error.

Answer

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.