我的http标头出现问题. 我需要发送带有空正文和标头"Content-Length:0"的后请求.但这似乎是不可能的,因为我得到了这个例外:
i have problems with my http-headers. I need to send a post-request with an empty body and the header "Content-Length:0". But this seems impossible, becauce i get this exception:
-org.apache.http.ProtocolException:Content-Length标头已经存在-
--org.apache.http.ProtocolException: Content-Length header already present--
但是当我不包括标题时,请求中没有Content-Length标题,因此我得到400:错误的请求. 我也用Python构建了这个文件,在这里也可以正常工作,但是我不能在Java中运行它.你能帮我吗?
But when i do not include the header, there is no Content-Length header in the request and therefore i get 400: Bad Request. I build this in Python too and there it works fine, but i can not make it work in java. Can you help me, please?
这是我的代码:
HttpClient client = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader(header..); httpPost.addHeader(another header); httpPost.addHeader("Content-Type","application/xml"); httpPost.addHeader("Content-Length","0"); httpPost.addHeader(another header); HttpResponse response = client.execute(httpPost); System.out.println(response.getStatusLine().getStatusCode());
解决了@Beno Arakelyan的问题.
Solved the problem with the answer from @Beno Arakelyan.
推荐答案我尝试使用Content-Length > http-request (基于apache http客户端构建).它工作正常.示例:
I tried to send the Content-Length with http-request which built on apache http client. It works fine. Example:
private static final HttpRequest<?> HTTP_REQUEST = HttpRequestBuilder.createGet(YOUR_URI) .addDefaultHeader(HttpHeaders.CONTENT_LENGTH, "0") .build(); public void test(){ System.out.println(HTTP_REQUEST.execute().getStatisCode()); // is 200 }更多推荐
Java Http
发布评论