Java Http

编程入门 行业动态 更新时间:2024-10-25 20:25:21
本文介绍了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.

推荐答案

我尝试使用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

本文发布于:2023-10-12 17:31:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!