如何解决“HTTP 错误 411。请求必须分块或具有内容长度。” 在 Java 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36189833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resolve "HTTP Error 411. The request must be chunked or have a content length." in java
提问by Aniruddha Das
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
我正在使用 HttpConnect 并尝试从服务器获取一些令牌。但是每当我尝试获得响应时,它总是说您没有设置内容长度或内容长度有问题,即使我尝试以多种不同方式设置内容长度
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();
采纳答案by Aniruddha Das
Moving away from HttpConnectto HttpClientworked for me. So I moved away from HttpURLConnectionand created an http HttpClientobject and call execute methods to get data from the server.
从移开HttpConnect来HttpClient对我来说有效。所以我离开HttpURLConnection并创建了一个 httpHttpClient对象并调用执行方法从服务器获取数据。
Below is the code which make http request using HttpClientrather HttpURLConnection
下面是使用HttpClient而不是发出http请求的代码HttpURLConnection
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(authUrl);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("phone", "phone");
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
String response = getResponseBody(inputStream);
System.out.println(response);
} catch (ClientProtocolException e) {
System.out.println("ClientProtocolException : " + e.getLocalizedMessage());
} catch (IOException e) {
System.out.println("IOException:" + e.getLocalizedMessage());
} catch (Exception e) {
System.out.println("Exception:" + e.getLocalizedMessage());
}
回答by user207421
You're setting a content-length but never sending a request body.
您正在设置内容长度,但从未发送请求正文。
Don'tset the content-length. Java does it for you.
不要设置内容长度。Java 为您做到了。
NB setDoOutput(true)sets the method to POST.
NBsetDoOutput(true)将方法设置为 POST。

