[英]How to assign content-length header in Java 11+ HTTPClient
I am trying to assign the content-length header in Java but it seems impossible.我正在尝试在 Java 中分配内容长度标头,但似乎不可能。
Here's my code:这是我的代码:
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("URL"))
.header("Content-Type","application/pdf")
.header("Authorization",String.format("Token token=%s",token))
.header("Transfer-Encoding","chunked")
.header("Content-Length",String.valueOf(contentLength))
.POST(HttpRequest.BodyPublishers.ofByteArray(file))
.build();
If I run this, then I get the exception "Restricted Header Name: "Content-Length". So then I remove the line where I set the content-length. At this point I get an IOException saying my request doesn't have a content-length header.如果我运行它,那么我会得到异常“Restricted Header Name:”Content-Length”。然后我删除了设置内容长度的行。此时我得到一个 IOException 说我的请求没有内容长度标头。
How the hell am I supposed to set the content-length header if the HttpClient throws an exception if I, you know, set the content length header?如果 HttpClient 抛出异常,如果我设置内容长度标头,我该怎么设置内容长度标头?
EDIT:编辑:
I know this doesn't solve the question, but I ended up trying non-system standard HTTP Client Libaries.我知道这不能解决问题,但我最终尝试了非系统标准 HTTP 客户端库。 OkHttp for some reason failed on me, but Apache's HttpClient worked fine. OkHttp 由于某种原因在我身上失败了,但 Apache 的 HttpClient 工作正常。 It's not as elegant as Java's built in client, but it actually worked.它不像 Java 的内置客户端那么优雅,但它确实有效。
(This is not really an answer, but the code is too long for a comment) (这不是真正的答案,但代码太长无法评论)
I tried this code (which is similar to your example), and the response from httpbin.org shows that the Java11 HTTPClient fills in the content-length header with the correct value:我尝试了这段代码(类似于您的示例),来自 httpbin.org 的响应显示 Java11 HTTPClient 用正确的值填充了 content-length 标头:
public class HR {
public static void main(String[] args) throws IOException, InterruptedException {
byte[] file = {0, 1, 2, 3, 4, 5, 6, 7};
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/anything"))
.header("Content-Type", "application/pdf")
.header("Authorization", "Token token=xyz")
.header("Transfer-Encoding", "chunked")
// .header("Content-Length", String.valueOf(file.length))
.POST(HttpRequest.BodyPublishers.ofByteArray(file))
.build();
HttpClient client = HttpClient.newHttpClient();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
The response from httpbin.org is (with some sensitive fields masked) httpbin.org 的响应是(屏蔽了一些敏感字段)
{
"args": {},
"data": "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007",
"files": {},
"form": {},
"headers": {
"Authorization": "Token token=xyz",
"Content-Length": "8",
"Content-Type": "application/pdf",
"Host": "httpbin.org",
"User-Agent": "Java-http-client/15.0.1"
},
"json": null,
"method": "POST",
"origin": "1.2.3.4",
"url": "https://httpbin.org/anything"
}
The response will show up twice (once from the async call, once from the synchronuous call).响应将出现两次(一次来自异步调用,一次来自同步调用)。
I'm therefore not sure on why your code doesn't send the content-length header, the HttpRequest.BodyPublishers.ofByteArray
should do that for you too.因此,我不确定为什么您的代码不发送内容长度标头, HttpRequest.BodyPublishers.ofByteArray
应该为您发送。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.