Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 490
Arasu Avatar asked Jan 04 '12 06:01

Arasu


People also ask

How do I fix Error 411?

Fortunately, you can easily fix the “411 Length Required” error. This HTTP status code happens when the server requires a content-length header, but it isn't specified in a request. To resolve this issue, you can simply define a content length.

What is a 411 response code?

What Is a 411 Status Code? The server refuses to accept the request without a defined Content-Length1.


1 Answers

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.

like image 110
Jon Skeet Avatar answered Sep 30 '22 04:09

Jon Skeet