Java Http-帖子标题内容长度 [英] Java Http - Post Header Content-Length

查看:82
本文介绍了Java Http-帖子标题内容长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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.

推荐答案

我尝试使用

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-帖子标题内容长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆