Java Apache HttpClient 响应内容长度返回 -1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18726892/
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
Apache HttpClient response content length returns -1
提问by PeterMoron
Why does the following Code returns -1? Seems that the request failed.
为什么以下代码返回 -1?好像请求失败了。
public static void main(String[] args)
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.google.de");
HttpResponse response;
try
{
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
// Prints -1
System.out.println(entity.getContentLength());
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
httpGet.releaseConnection();
}
}
And is it possible to get the response as String?
是否可以将响应作为字符串?
采纳答案by Sotirios Delimanolis
Try running
尝试跑步
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
System.out.println(header);
}
It will print
它会打印
Date: Tue, 10 Sep 2013 19:10:04 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=dad7e2356ddb3b7a:FF=0:TM=1378840204:LM=1378840204:S=vQcLzVPbOOTxfvL4; expires=Thu, 10-Sep-2015 19:10:04 GMT; path=/; domain=.google.de
Set-Cookie: NID=67=S11HcqAV454IGRGMRo-AJpxAPxClJeRs4DRkAJQ5vI3YBh4anN3qS0EVeiYX_4XDTGN-mY86xTBoJ3Ncca7eNSdtGjcaG31pbCOuqsZEQMWwKn-7-6Dnizx395snehdA; expires=Wed, 12-Mar-2014 19:10:04 GMT; path=/; domain=.google.de; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 80:quic
Transfer-Encoding: chunked
This is not a problem, the page you requested simply doesn't provide a Content-Length
header in its response. As such, the HttpEntity#getContentLength()
returns -1
.
这不是问题,您请求的页面根本没有Content-Length
在其响应中提供标头。因此,HttpEntity#getContentLength()
返回-1
.
EntityUtils
has a number of methods, some of which return a String
.
EntityUtils
有许多方法,其中一些返回 a String
。
Running curl
more recently produces
运行curl
最近产生
> curl --head http://www.google.de
HTTP/1.1 200 OK
Date: Fri, 03 Apr 2020 15:38:18 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2020-04-03-15; expires=Sun, 03-May-2020 15:38:18 GMT; path=/; domain=.google.de; Secure
Set-Cookie: NID=201=H8GdKY8_vE5Ehy6qSkmQru13HqdGEj2tvZUFqvTDAVBxFoL4POI0swPtfI45v1TBjrJuAAfbcNMUddniIf9HHituCAFwUqmUFMDwxDYK5qUlcWiB1A64OcGp6PTT6LKur2r_3z-ToSvLf8RZhKWdny6E8SaArMpkaOqUEWp4aoQ; expires=Sat, 03-Oct-2020 15:38:18 GMT; path=/; domain=.google.de; HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding
The headers contain a Transfer-Encoding
value of chunked
. With chunked
, the response contains "chunks" preceded by their length. An HTTP client uses those to read the entire response.
标头包含的Transfer-Encoding
值为chunked
。使用chunked
,响应包含以它们的长度开头的“块”。HTTP 客户端使用这些来读取整个响应。
The HTTP Specification statesthat the Content-Length
header should not be present when Transfer-Encoding
has a value of chunked
and MUST be ignored if it is.
的HTTP规范规定,所述Content-Length
时标头应不存在Transfer-Encoding
具有值chunked
并且如果它是必须被忽略。
回答by gaojun1000
If you really want to get the content length without caring about the content, you can do this.
如果你真的想在不关心内容的情况下获得内容长度,你可以这样做。
EntityUtils.toByteArray(httpResponse.getEntity()).length
EntityUtils.toByteArray(httpResponse.getEntity()).length
回答by xici
Please notice that response header name Transfer-Encoding. Its value is chunked which means data is deliveryed block by block. Transfer-Encoding: chunked and Content-Length does not turn out at the same time. There are two reason.
请注意响应头名称 Transfer-Encoding。它的值是分块的,这意味着数据是逐块传递的。Transfer-Encoding: chunked 和 Content-Length 不会同时出现。有两个原因。
- Server does not want sent content length.
- Or server do not know the content length when it flush a big size data whose size is large than server's buffer.
- 服务器不想要发送的内容长度。
- 或者服务器在刷新大于服务器缓冲区的大数据时不知道内容长度。
So when there is no content length header, you can find the size of each chunked block before body of content. For example:
因此,当没有内容长度标头时,您可以在内容正文之前找到每个分块块的大小。例如:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=8A7461DDA53B4C4DD0E89D73219CB5F8; Path=/
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 18 Mar 2015 07:10:05 GMT
11
helloworld!
3
123
0
Above headers and content tell us, there are two block data. The size of first block is 11. the size of second block is 3. So the content length is 14 at all.
上面的 headers 和 content 告诉我们,有两个区块数据。第一个块的大小是 11。第二个块的大小是 3。所以内容长度是 14。
regards, Xici
问候, 西慈