'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.
Solution 1:[1]
Try to add the following lines to your code, that might help you understand the problem a bit better :
conn.setRequestProperty("Content-Length", "0");
Do check what your inputStream from your HTTP ERROR 411 states by adding this code :
InputStream is = null;
if (conn.getResponseCode() != 200)
{
is = conn.getErrorStream();
}
else
{
is = conn.getInputStream();
}
Hopefully that might help.
Regards
Solution 2:[2]
411 - Length Required
The 411 status code occurs when a server refuses to process a request because a content length was not specified.
refer for details
Solution 3:[3]
Adding the following line in the code where post is performed worked:
conn.setRequestProperty("Content-Length", "0");
Solution 4:[4]
POST/ PUT should be use when modifying or creating a new instance on the back-end. When using REST call in the form on http://///{parameter1}/{parameter2} (and so on), no query or body send! still it should be POST call if it modifying 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);
}
In this way we don't harm the existing model and the call will be sent with the ZERO length header.
Solution 5:[5]
I passed an empty object "{}" in the body this fixed it.
HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/json");
OutputStreamWriter writers = new OutputStreamWriter(connection.getOutputStream());
writers.write("{}"); //body
writers.flush();
instead of this you can set Content-length as 0 also as suggested in other answers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nIcE cOw |
| Solution 2 | Hemant Metalia |
| Solution 3 | Dibyendu Mitra Roy |
| Solution 4 | Amour Shmuel |
| Solution 5 | MOHANTEJA |
