httpclient: Content-Length header already present问题

现象:用httpclient发送http请求时,客户端返回:

org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:822)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
……
Caused by: org.apache.http.ProtocolException: Content-Length header already present
at org.apache.http.protocol.RequestContent.process(RequestContent.java:67)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:108)
at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:174)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:462)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
... 27 more

原因:查看httpclient中RequestContent类的process方法,看到当body非空时,会自动加上Content-Length请求头及其对应值,不需要自己手动加上它。源码如下:

</pre>
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (request instanceof HttpEntityEnclosingRequest) {
if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (request.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
if (entity == null) {
request.addHeader(HTTP.CONTENT_LEN, "0");
return;
}
// Must specify a transfer encoding or a content length
if (entity.isChunked() || entity.getContentLength() < 0) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
throw new ProtocolException(
"Chunked transfer encoding not allowed for " + ver);
}
request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else {
request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !request.containsHeader(
HTTP.CONTENT_TYPE )) {
request.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !request.containsHeader(
HTTP.CONTENT_ENCODING)) {
request.addHeader(entity.getContentEncoding());
}
}
}
解决方法:remove掉Content-Length后,不再抛异常,请求正常返回。

https://github.com/square/retrofit/issues/454

时间: 2024-12-28 15:03:58

httpclient: Content-Length header already present问题的相关文章

As.net WebAPI CORS, 开启跨源访问,解决错误No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource

默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:22852/api/values, What is "Same Origin"? Two URLs have the same origin if they have identical schemes, hosts, and ports. (RFC 6454) These two URLs h

js跨域访问,No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource

js跨域访问提示错误:XMLHttpRequest cannot load http://...... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 解决方法: 1.如果请求的url是aspx页面,则需要在aspx页面中添加代码:Response.AddHeader("Access-Contro

No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource

一.现象 ajax调用请求后,前端提示收到这个错误 No 'Access-Control-Allow-Origin' header is present on the requested resource 二.原因 这是一个典型的跨域请求失败的例子. 三.解决 服务器的响应消息中增加头字段即可. rsp.addHeader("Access-Control-Allow-Origin", "*"); 四.扩展 更多的关于CORS的内容可以学习这篇文章: https://

WCF常见异常-The maximum string content length quota (8192) has been exceeded while reading XML data

异常信息:The maximum string content length quota (8192) has been exceeded while reading XML data 问题:调用第三方的WCF服务,产生上述异常信息 分析: 在公布WCF host端时,要确保host端以及客户端的设置允许一定大小的数据传输. 如果未设置传输大小,maxStringContentLength默认大小为8192. 1)如果第三方服务未设置maxStringContentLength或者设置的maxS

jQuery 跨域访问的三种方式 No &#39;Access-Control-Allow-Origin&#39; header is present on the reque

问题: XMLHttpRequest cannot load http://v.xxx.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. test.html:1 Resource interpreted as Script but transferred

The maximum string content length quota (8192) has been exceeded while reading XML data

原文:The maximum string content length quota (8192) has been exceeded while reading XML data 问题场景:在我们WCF服务发布后,我们要确保服务端以及客户端的配置文件允许合适大小的传输设置.笔者在发布WCF服务时,服务端的绑定未做传输大小的设置(采用了默认,maxStringContentLength默认大小为8192),而我们在传输序列化的数据时,大小超过了这个限制. 读取 XML 数据时,超出最大字符串内容

the request doesn&#39;t contain a multipart/form-data or multipart/form-data stream, content type header

the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header 一,HTTP上传的基本知识 在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型.下边是说明: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码格式. mult

ajax请求node.js接口时出现 No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource错误

ajax请求node.js接口出现了如下的错误: XMLHttpRequest cannot load http://xxx.xxx.xx.xx:8888/getTem?cityId=110105&date=2015-03-04. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 百度了一下,原来是

ABP PUT、DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法(HTTP 谓词) 引发客户端错误 No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource

先请检查是否是跨域配置问题,请参考博客:http://www.cnblogs.com/donaldtdz/p/7882225.html 一.问题描述 ABP angular前端部署后,查询,新增都没问题,但更新和删除会报一个跨域问题的错误,详细信息如下: PUT http://localhost:8060/api/services/app/User/Update 405 (Method Not Allowed) users:1 Failed to load http://localhost:80