You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "jason zhang (Created) (JIRA)" <ji...@apache.org> on 2012/03/15 02:16:41 UTC
[jira] [Created] (HTTPCLIENT-1174) when using HttpResponse's API
getEntity return HttpEntity instance to get content length, HttpEntity's
function API getContentLength() CAN NOT return correct content length.
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: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
[jira] [Commented] (HTTPCLIENT-1174) when using HttpResponse's API
getEntity return HttpEntity instance to get content length, HttpEntity's
function API getContentLength() CAN NOT return correct content length.
Posted by "jason zhang (Commented) (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/HTTPCLIENT-1174?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13230771#comment-13230771 ]
jason zhang commented on HTTPCLIENT-1174:
-----------------------------------------
Hi, Oleg, another issue need to mention is when I use fiddle2 to inspect the session, It shows that response head content length is 6400. If this result is correct, does it mean the getContentLength() of HttpEntity is not reliable, while we use this for output content from response?
thank you for your time.
Regards!
> 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: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
[jira] [Commented] (HTTPCLIENT-1174) when using HttpResponse's API
getEntity return HttpEntity instance to get content length, HttpEntity's
function API getContentLength() CAN NOT return correct content length.
Posted by "Oleg Kalnichevski (Commented) (JIRA)" <ji...@apache.org>.
[ 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: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org
[jira] [Resolved] (HTTPCLIENT-1174) when using HttpResponse's API
getEntity return HttpEntity instance to get content length, HttpEntity's
function API getContentLength() CAN NOT return correct content length.
Posted by "Oleg Kalnichevski (Resolved) (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/HTTPCLIENT-1174?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Oleg Kalnichevski resolved HTTPCLIENT-1174.
-------------------------------------------
Resolution: Invalid
---
2012/03/15 09:19:52:788 CET [DEBUG] SingleClientConnManager - Get connection for route HttpRoute[{}->http://maps.googleapis.com]
2012/03/15 09:19:54:998 CET [DEBUG] DefaultClientConnectionOperator - Connecting to maps.googleapis.com:80
2012/03/15 09:19:55:140 CET [DEBUG] RequestAddCookies - CookieSpec selected: best-match
2012/03/15 09:19:55:168 CET [DEBUG] RequestAuthCache - Auth cache not set in the context
2012/03/15 09:19:55:169 CET [DEBUG] DefaultHttpClient - Attempt 1 to execute request
2012/03/15 09:19:55:170 CET [DEBUG] DefaultClientConnection - Sending request: GET /maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA%7CLexington,MA&sensor=false HTTP/1.1
2012/03/15 09:19:55:174 CET [DEBUG] headers - >> GET /maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA%7CLexington,MA&sensor=false HTTP/1.1
2012/03/15 09:19:55:175 CET [DEBUG] headers - >> Host: maps.googleapis.com
2012/03/15 09:19:55:175 CET [DEBUG] headers - >> Connection: Keep-Alive
2012/03/15 09:19:55:176 CET [DEBUG] headers - >> User-Agent: Apache-HttpClient/4.1.3 (java 1.5)
2012/03/15 09:19:55:434 CET [DEBUG] DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
2012/03/15 09:19:55:435 CET [DEBUG] headers - << HTTP/1.1 200 OK
2012/03/15 09:19:55:436 CET [DEBUG] headers - << Content-Type: application/xml; charset=UTF-8
2012/03/15 09:19:55:436 CET [DEBUG] headers - << Transfer-Encoding: chunked
2012/03/15 09:19:55:437 CET [DEBUG] headers - << Cache-Control: public, max-age=86400
2012/03/15 09:19:55:437 CET [DEBUG] headers - << Date: Thu, 15 Mar 2012 08:19:55 GMT
2012/03/15 09:19:55:437 CET [DEBUG] headers - << Expires: Fri, 16 Mar 2012 08:19:55 GMT
2012/03/15 09:19:55:437 CET [DEBUG] headers - << Server: mafe
2012/03/15 09:19:55:438 CET [DEBUG] headers - << Vary: Accept-Language
2012/03/15 09:19:55:438 CET [DEBUG] headers - << X-Frame-Options: SAMEORIGIN
2012/03/15 09:19:55:439 CET [DEBUG] headers - << X-XSS-Protection: 1; mode=block
2012/03/15 09:19:55:454 CET [DEBUG] DefaultHttpClient - Connection can be kept alive indefinitely
2012/03/15 09:19:55:943 CET [DEBUG] SingleClientConnManager - Releasing connection org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@54281d4b
---
The server responds with a chunk coded response, so HttpClient is absolutely correct in returning -1 (not known) as response entity's content length.
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: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org