java 设置 HTTP POST 请求的内容长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11663407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:57:15  来源:igfitidea点击:

Setting content length of an HTTP POST request

javapostapache-httpclient-4.xhttp-content-length

提问by Umer Hayat

I am trying to make a Http POST request using apache HTTP client. I am trying to copy contents of an HTTP POST request (received at my application) to another HTTP POST request (initiated from my application to another URL). Code is shown below:

我正在尝试使用 apache HTTP 客户端发出 Http POST 请求。我正在尝试将 HTTP POST 请求(从我的应用程序接收)的内容复制到另一个 HTTP POST 请求(从我的应用程序启动到另一个 URL)。代码如下所示:

httpPost = new HttpPost(inputURL);
// copy headers
for (Enumeration<String> e = request.getHeaderNames(); e.hasMoreElements();) {
      String headerName = e.nextElement().toString(); 
      httpPost.setHeader(headerName, request.getHeader(headerName));
}

BufferedInputStream clientToProxyBuf = new BufferedInputStream(request.getInputStream());
BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
basicHttpEntity.setContent(clientToProxyBuf);
basicHttpEntity.setContentLength(clientToProxyBuf.available());

httpPost.setEntity(basicHttpEntity);

HttpResponse responseFromWeb = httpclient.execute(httpPost);

Basically, I am trying to implement a proxy application which will get a url as parameter, froward the request to the URL and then serve pages etc in custom look and feel.

基本上,我正在尝试实现一个代理应用程序,它将获取一个 url 作为参数,将请求转发到该 URL,然后以自定义外观提供页面等。

Here requestis HttpServletRequest. I am facing problem in setting content length. Through debugging I found out that clientToProxyBuf.available()is not giving me correct length of input stream and I am getting Http error 400 IE and Error 354 (net::ERR_CONTENT_LENGTH_MISMATCH): The server unexpectedly closed the connectionin chrome.

这里request是 HttpServletRequest。我在设置内容长度时遇到问题。通过调试,我发现它clientToProxyBuf.available()没有给我正确的输入流长度,我在 IE 和Error 354 (net::ERR_CONTENT_LENGTH_MISMATCH): The server unexpectedly closed the connectionchrome 中收到 Http 错误 400 。

Am I doing it wrong? Is there any other way to achieve it?

我做错了吗?有没有其他方法可以实现它?

采纳答案by Umer Hayat

It was rather simple and very obvious. I just needed to get content length from header as:

这很简单,也很明显。我只需要从标题中获取内容长度:

basicHttpEntity.setContentLength(Integer.parseInt(request.getHeader("Content-Length")));

回答by Pradeep Pati

The available()function doesn't provide the actual length of the content of the stream, rather

available()函数不提供流内容的实际长度,而是

Returns the number of bytes that can be read from this input stream without blocking. (From javadoc)

返回可以在不阻塞的情况下从此输入流读取的字节数。(来自javadoc

I would suggest you to first read the whole content from the stream, and then set that to the content, rather than passing the stream object. That way, you will also have the actual length of the content.

我建议您首先从流中读取整个内容,然后将其设置为内容,而不是传递流对象。这样,您还将获得内容的实际长度。