[
https://issues.apache.org/jira/browse/HTTPCLIENT-1174?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13231179#comment-13231179
]
Oleg Kalnichevski commented on HTTPCLIENT-1174:
-----------------------------------------------
HttpEntity#getContentLenth is reliable but it is merely a hint. It returns
non-negative value only if the opposite endpoint delineated the content body
with the Content-Length header. If the content body is chunked, it's length
cannot be known until fully consumed.
Oleg
> when using HttpResponse's API getEntity return HttpEntity instance to get
> content length, HttpEntity's function API getContentLength() CAN NOT return
> correct content length.
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: HTTPCLIENT-1174
> URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1174
> Project: HttpComponents HttpClient
> Issue Type: Bug
> Components: HttpClient
> Affects Versions: 4.1.3
> Environment: window xp sp3,java 7.0, Eclipse Java EE IDE for Web
> Developers.Version: Indigo Service Release 1,Build id: 20110916-0149
> Reporter: jason zhang
>
> when using HttpClient get xml data from google API, the response's content
> length is 6400, but HttpEntity's function getContentLength() is -1. sample
> test is bellow.
> ============
> import java.io.InputStream;
> import org.apache.http.HttpEntity;
> import org.apache.http.HttpResponse;
> import org.apache.http.client.HttpClient;
> import org.apache.http.client.methods.HttpGet;
> import org.apache.http.impl.client.DefaultHttpClient;
> import org.apache.http.util.EntityUtils;
> public class testGetContentLength {
> /**
> * @param args
> */
> public static void main(String[] args) {
> HttpClient httpclient = new DefaultHttpClient();
> HttpGet httpget = new
> HttpGet("http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA%7CLexington,MA&sensor=false");
> try{
> HttpResponse response = httpclient.execute(httpget);
> HttpEntity entity = response.getEntity();
> if (entity != null) {
> long len = entity.getContentLength();
> System.out.println("length:="+len);
> if (len != -1) {
> System.out.println(EntityUtils.toString(entity));
> } else {
> // Stream content out
> InputStream instream = entity.getContent();
> int l;
> byte[] tmp = new byte[512];
> while ((l = instream.read(tmp)) != -1) {
> System.out.println(new String(tmp,0,l));
>
> }
> //
> // BufferedInputStream in = new
> BufferedInputStream(instream);
> // StringBuffer buffer = new StringBuffer();
> // while((l=in.read(tmp))!=-1){
> // buffer.append(new String(tmp,0,l));
> // }
> // System.out.println(buffer.toString());
> }
> }
> }catch(Exception e){
> e.printStackTrace();
> }
> }
> }
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]