Estoy OBTENIENDO una página con Apache HttpClient y quiero almacenar el cuerpo http de la respuesta del servidor en una cadena para poder manipular esta cadena y imprímalo en la consola.Apache HttpClient en Java, instream.toString = org.apache.http.conn.EofSensorInputStream
Desafortunadamente cuando se ejecuta este método me sale este mensaje de nuevo:
17:52:01,862 INFO Driver:53 - fetchPage STARTING
17:52:07,580 INFO Driver:73 - fetchPage ENDING, took 5716
[email protected]
La Clase fetchPage:
public String fetchPage(String part){
log.info("fetchPage STARTING");
long start = System.currentTimeMillis();
String reply;
String searchurl = URL + URL_SEARCH_BASE + part + URL_SEARCH_TAIL;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(searchurl);
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
}
long elapsedTimeMillis = System.currentTimeMillis()-start;
log.info("fetchPage ENDING, took " + elapsedTimeMillis);
reply = instream.toString();
System.out.println(reply);
return reply;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
La clave para entender aquí es que 'toString()' en una 'InputStream 'no es un método para leer su contenido como' Cadena', sino más bien para obtener una representación de cadena simple del objeto en sí. Normalmente (incluido en este caso) un 'InputStream' no tiene una representación de cadena útil que pueda proporcionar, por lo que solo utiliza el' Object.toString() 'predeterminado. – ColinD