[英]Jersey Client (2.4) - PUT InputStream but Content-Length = 0
I'm trying to make a request to a Jersey REST service
which accepts an InputStream
as an entity using the PUT
method. 我正在尝试向
Jersey REST service
发出请求,该Jersey REST service
使用PUT
方法将InputStream
作为实体接受。
When I do, the Jersey client sets the Content-Length
header to 0. 当我这样做时,Jersey客户端将
Content-Length
标头设置为0。
The REST service requires that the Content-Length
is set to the actual size of the stream (IE the size of the file), but, whenever I try to set the Content-Length
the client throws: REST服务要求将
Content-Length
设置为流的实际大小(即文件的大小),但是,每当我尝试设置Content-Length
,客户端都会抛出:
org.apache.http.ProtocolException: Content-Length header already present
org.apache.http.ProtocolException:Content-Length标头已经存在
To note - the exception is not thrown when I add the Content-Length header, (at that point, this header doesn't exist), it's thrown (I assume) when Jersey / Apache HTTPClient
tries to add the 0
header value later. 注意-添加Content-Length标头时不会引发异常(此时该标头不存在),当
Jersey / Apache HTTPClient
尝试添加0
标头值时,将引发(我假设)此异常。
Stack if it's any help: 堆叠是否有帮助:
Caused by: org.apache.http.client.ClientProtocolException at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:827) at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:326) ... 40 more
造成原因:org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909)的org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.org)上的org.apache.http.client.ClientProtocolException java:827),网址为org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:326)...还有40
Caused by: org.apache.http.ProtocolException: Content-Length header already present at org.apache.http.protocol.RequestContent.process(RequestContent.java:96) at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:109) at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:176) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:518) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) ... 42 more
由以下原因引起:org.apache.http.ProtocolException:Content-Length标头已存在于org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor)上的org.apache.http.protocol.RequestContent.process(RequestContent.java:96) .java:109),位于org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:176),位于org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:518),位于org.apache。 http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)...还有42
So, any ideas why Jersey / Apache HTTP Client
sets the Content-Length
header to 0, and, why I'm not able to override the Content-Length
header 因此,所有关于为何
Jersey / Apache HTTP Client
将Content-Length
标头设置为0,以及为什么我无法覆盖Content-Length
标头的想法
I'm using: 我正在使用:
Jersey 2.4.1
with the ApacheConnector
带有
ApacheConnector
Jersey 2.4.1
Thanks in advance 提前致谢
Will 将
Just to answer this: 只是为了回答这个问题:
This is a problem with the ApacheConnector, which will always return -1 for the size of the content, ensuring this value is never set. 这是ApacheConnector的问题,它将始终为内容的大小返回-1,以确保从未设置此值。 (This is hardcoded in the getHttpEntity method)
(这在getHttpEntity方法中进行了硬编码)
Therefore, if the content-length header is required, one either has to reimplement the ApacheConnector as described here or use a different connector. 因此,如果需要的Content-Length头,一个或者具有重新实现ApacheConnector如所描述的此处或使用不同的连接器。
I have reported this on the Jersey jira. 我已经在泽西岛吉拉报道了这一点。
https://java.net/jira/browse/JERSEY-2373 https://java.net/jira/browse/JERSEY-2373
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.