Reputation: 2148
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.
Upvotes: 9
Views: 33916
Reputation: 169
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.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1665
Adding the following line in the code where post is performed worked:
conn.setRequestProperty("Content-Length", "0");
Upvotes: 2
Reputation: 24616
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
Upvotes: 5
Reputation: 1500385
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.
Upvotes: 8