Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve "HTTP Error 411. The request must be chunked or have a content length." in java

I am using HttpConnect and trying to get some token from the server. But whenever I try to get the response, its always saying you have not set or problem with content length even I tried to set the content length in many different ways

conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("X-DocuSign-Authentication", httpAuthHeader);
conn.setRequestProperty("Accept", "application/json");
if (method.equalsIgnoreCase("POST")) {
  conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  conn.setRequestProperty("Content-Length", Integer.toString(body.length()));
            conn.setDoOutput(true);
}


status = conn.getResponseCode(); // triggers the request
if (status != 200) { //// 200 = OK 
    errorParse(conn, status);
    return;
}

InputStream is = conn.getInputStream();
like image 811
Aniruddha Das Avatar asked Mar 23 '16 22:03

Aniruddha Das


People also ask

What does HTTP Error 411 mean?

The HyperText Transfer Protocol (HTTP) 411 Length Required client error response code indicates that the server refuses to accept the request without a defined Content-Length header.


1 Answers

You're setting a content-length but never sending a request body.

Don't set the content-length. Java does it for you.

NB setDoOutput(true) sets the method to POST.

like image 116
user207421 Avatar answered Oct 14 '22 17:10

user207421