Jersey 2.0 Content-Length未设置 [英] Jersey 2.0 Content-Length not set
问题描述
我正在尝试发布到需要使用以下代码设置Content-Length标头的网络服务:
I'm trying to post to a web service that requires the Content-Length header to be set using the following code:
// EDIT: added apache connector code
ClientConfig clientConfig = new ClientConfig();
ApacheConnector apache = new ApacheConnector(clientConfig);
// setup client to log requests and responses and their entities
client.register(new LoggingFilter(Logger.getLogger("com.example.app"), true));
Part part = new Part("123");
WebTarget target = client.target("https://api.thing.com/v1.0/thing/{thingId}");
Response jsonResponse = target.resolveTemplate("thingId", "abcdefg")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "anauthcodehere")
.post(Entity.json(part));
来自发行说明 https://java.net/jira/browse/JERSEY-1617 和Jersey 2.0文档 https://jersey.java.net/documentation/latest/message-body-workers.html 表示Content-Length是自动设置的。但是,我从服务器返回411响应代码,表明请求中没有Content-Length。
From the release notes https://java.net/jira/browse/JERSEY-1617 and the Jersey 2.0 documentation https://jersey.java.net/documentation/latest/message-body-workers.html it implies that Content-Length is automatically set. However, I get a 411 response code back from the server indicating that Content-Length is not present in the request.
有没有人知道获取内容的最佳方式 - 长度标头设置?
Does anyone know the best way to get the Content-Length header set?
我已经通过设置记录器验证了请求中没有生成Content-Length标头。
I've verified through setting up a logger that the Content-Length header is not generated in the request.
谢谢。
推荐答案
我使用Jersey Client 2.2和Netcat进行了快速测试,它正在向我展示即使LoggingFilter没有报告它,Jersey也会发送Content-Length标头。
I ran a quick test with Jersey Client 2.2 and Netcat, and it is showing me that Jersey is sending the Content-Length header, even though the LoggingFilter is not reporting it.
为了做这个测试,我首先在一个shell中运行netcat。
To do this test, I first ran netcat in one shell.
nc -l 8090
然后我在另一个shell中执行了以下Jersey代码。
Then I executed the following Jersey code in another shell.
Response response = ClientBuilder.newClient()
.register(new LoggingFilter(Logger.getLogger("com.example.app"), true))
.target("http://localhost:8090/test")
.request()
.post(Entity.json(IOUtils.toInputStream("{key:\"value\"}")));
运行此代码后,会记录以下行。
After running this code, the following lines get logged.
INFO: 1 * LoggingFilter - Request received on thread main
1 > POST http://localhost:8090/test
1 > Content-Type: application/json
{key:"value"}
但是,netcat报告消息中的多个标题。
However, netcat reports several more headers in the message.
POST /test HTTP/1.1
Content-Type: application/json
User-Agent: Jersey/2.0 (HttpUrlConnection 1.7.0_17)
Host: localhost:8090
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 13
{key:"value"}
我在OSX上用Java6和Java7运行了这个测试,结果相同。我也在Jersey 2.0中进行了测试,结果相似。
I ran this test on OSX with Java6 and Java7, with the same results. I also ran the test in Jersey 2.0, with similar results.
这篇关于Jersey 2.0 Content-Length未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!