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.
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 Status Code? The server refuses to accept the request without a defined Content-Length1.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With