简体   繁体   English

在Jersey客户端请求中未设置Content-Length-Header

[英]Content-Length-Header not set in Jersey Client request

I'm using Jersey Client to access a webservice, like this: 我正在使用Jersey Client访问web服务,如下所示:
response = r.accept(MediaType.TEXT_PLAIN_TYPE).header("content-length", 0).post(String.class);
where r is a WebResource 其中r是WebResource

However, the Webservice returns 411 - Content-Length is missing. 但是,Webservice返回411 - 缺少Content-Length。

using tcpdump, i found out that i am able to specify custom headers, ie .header("myheader", 0) works fine. 使用tcpdump,我发现我能够指定自定义标头,即.header("myheader", 0)工作正常。

So it seems that jersey is deleting the content-length header for some reasons. 因此,由于某些原因,球衣似乎正在删除内容长度标题。

Anyone has any ideas? 有人有什么想法吗?

I actually had a requirement to use an empty POST request for a Restful webservice. 我实际上要求对Restful webservice使用空的POST请求。

If you specify an empty string as the second parameter of post method, Jersey will create the Content-Length header with the value of 0. 如果指定空字符串作为post方法的第二个参数,Jersey将创建值为0的Content-Length标头。

eg response = r.accept(MediaType.TEXT_PLAIN_TYPE).post(String.class, ""); 例如response = r.accept(MediaType.TEXT_PLAIN_TYPE).post(String.class, "");

The content length of a call is computed by Jersey Client, it cannot be set. 调用的内容长度由Jersey Client计算,不能设置。 Paul Sandoz — a well known commiter on the project — have answered a similar question : Paul Sandoz--该项目的知名提交者 - 回答了类似的问题

Q: I think that "Content-Length" header is not being set automatically. 问:我认为“Content-Length”标题没有自动设置。

A: The Jersey runtime [...] determine the length of content. 答: Jersey运行时[...]确定内容的长度。

If you need further informations, please explain what result did you expect from POSTing a String to a Resource with an empty size. 如果您需要进一步的信息,请解释您将字符串POST到空白大小的资源时所期望的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM