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

I check if the internet is accessible 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(); 

When I run this code, I get an exception

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

What could be the mistake.

+7
source share
5 answers

An HTTP 411 status code means "required length" - you tried to execute a POST request, but you never provided any input. The Java client code does not set the Content-Length header, and the server rejects the POST request without length.

Why are you even trying to make a message? Why not make a GET request or even better HEAD?

I would also recommend if you really need to know if a particular site (such as a web service) is running that you are connecting to, not just Google.

+6
source

Try adding the following lines to your code that may help you understand the problem a little better:

  conn.setRequestProperty("Content-Length", "0"); 

Verify that your input stream is from HTTP ERROR 411 by adding this code:

 InputStream is = null; if (conn.getResponseCode() != 200) { is = conn.getErrorStream(); } else { is = conn.getInputStream(); } 

Hope this helps.

Yours faithfully

+5
source

411 - Required Length

The status code 411 occurs when the server refuses to process the request because the length of the content is not specified.

See details

+2
source

Adding the following line to the code in which the post is executed:

 conn.setRequestProperty("Content-Length", "0"); 
+2
source

POST / PUT should be used when modifying or creating a new instance on the internal server. When using a REST call in the form at http: ///// {parameter1} / {parameter2} (etc.) A request or sending a body is not sent! it should still be a POST call if it changes the data.

So, in this case, we can do some reflection.

 String urlParameters = url.getQuery(); if (urlParameters == null) urlParameters = ""; byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; if (postDataLength > 0) { //in case that the content is not empty conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); } else { // Reflaction the HttpURLConnectioninstance Class<?> conRef = conn.getClass(); // Fetch the [requests] field, Type of MessageHeader Field requestsField= conRef .getDeclaredField("requests"); // The [requests] field is private, so we need to allow accessibility requestsField.setAccessible(true); MessageHeader messageHeader = (MessageHeader) requestsField.get(conn); // Place the "Content-Length" header with "0" value messageHeader.add("Content-Length", "0"); // Inject the modified headers requestsField.set(conn, messageHeader); } 

Thus, we will not harm the existing model, and the call will be sent with a ZERO-length header.

0
source

All Articles