【问题标题】:Java Http - Post Header Content-LengthJava Http - 发布标头内容长度
【发布时间】:2018-02-27 10:53:16
【问题描述】:

我的 http-headers 有问题。 我需要发送一个带有空正文和标题“Content-Length:0”的后请求。但这似乎是不可能的,因为我得到了这个例外:

--org.apache.http.ProtocolException: Content-Length 标头已经存在--

但是当我不包含标头时,请求中没有 Content-Length 标头,因此我得到 400:错误请求。 我也用 Python 构建了它,它工作得很好,但我不能让它在 java 中工作。你能帮帮我吗?

这是我的代码:

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 的回答解决了这个问题。

【问题讨论】:

  • FWIW,如果您不发送请求正文,则发送 Content-Type 没有任何意义。
  • 您的评论没有帮助@Reschke。我们可以讨论这篇文章(它执行一个不需要主体的动作),但这不是我的 api。我只需要使用这个 api,我不能改变它。如果可以的话,我会改变的不仅仅是这个:D
  • 您确定需要发送 Content-Length 吗?

标签: java http


【解决方案1】:

我尝试使用基于 apache http 客户端的http-request 发送Content-Length。它工作正常。示例:

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
}

【讨论】:

  • 我会测试它;)感谢您的意见,我会提供反馈
  • @TheJed 很高兴为您提供帮助
  • 有什么方法可以检查完整的请求,那么发送到服务器的是什么?在我看来,这应该可以工作,但它也失败了(正如我提到的,我有一个工作的 python 脚本,我发送一个带有我的标题和一个空正文的发布请求并且它工作,所以错误必须在我的 java - 代码中) .它和你完全一样,只是以同样的方式添加了其他标题。
  • 您是否收到相同的错误消息?您希望服务器上有什么请求正文/标头?您可以通过单元测试检查完整的请求。
  • 我调试了一下,请求将发送到服务器,因为没有标头(大小 = 0)。因此,我再次收到 400 - 错误请求。它们在 HTTP_REQUEST 中,但是在执行过程中,模块会构建一个 org.apache.http.client.methods.RequestBuilder 并且来自此的请求将发送到服务器,但没有我的标头。您确定该模块在没有主体的情况下可以正常工作吗?
【解决方案2】:

您无需计算内容长度,客户端会为您处理。尝试删除标题并设置正文,例如:httpPost.setEntity(new StringEntity(""))

【讨论】:

  • 已经试过了,但还是不行。在这种情况下,标题不包括在内:(
【解决方案3】:

这个Link directs you to the example below

URL url = new URL("https://reqbin.com/echo/post/json");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Content-Type", "application/json");

String data = "{\"widget\": {\n    \"window\": {\n        \"title\": \"Sample Widget\",\n        \"name\": \"sample_widget\",\n        \"width\": 600,\n        \"height\": 400\n    },\n    \"image\": { \n        \"src\": \"images/test.png\",\n        \"name\": \"sample_test\",\n        \"hOffset\": 150,\n        \"vOffset\": 150,\n        \"alignment\": \"center\"\n    },\n    \"text\": {\n        \"data\": \"Click Here\",\n        \"size\": 36,\n        \"style\": \"bold\",\n        \"name\": \"sample_click\",\n        \"alignment\": \"center\"\n    }\n}} ";

byte[] out = data.getBytes(StandardCharsets.UTF_8);

OutputStream stream = http.getOutputStream();
stream.write(out);

System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
http.disconnect();

注意:Content-Length 会自动添加。

【讨论】:

    猜你喜欢
    • 2013-01-23
    • 2012-04-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2012-07-13
    • 2022-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多