简体   繁体   English

Apache HttpClient in Java, instream.toString = org.apache.http.conn.EofSensorInputStream

[英]Apache HttpClient in Java, instream.toString = org.apache.http.conn.EofSensorInputStream

I am GETting a page with Apache HttpClient and I want to store the server reply's http body into a string so I can then manipulate this string and print it to the console.我正在获取一个带有 Apache HttpClient 的页面,我想将服务器回复的 http 正文存储到一个字符串中,这样我就可以操作这个字符串并将其打印到控制台。

Unfortunately when running this method I get this message back:不幸的是,在运行此方法时,我收到此消息:

17:52:01,862  INFO Driver:53 - fetchPage STARTING
17:52:07,580  INFO Driver:73 - fetchPage ENDING, took 5716
org.apache.http.conn.EofSensorInputStream@5e0eb724

The fetchPage Class: fetchPage Class:

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;
}

You are calling the toString on the InputStream after it has already read through.您在InputStream上调用toString在它已经读完之后。 You need to create your string from the byte arrays.您需要从字节 arrays 创建字符串。 The simpler way to get the String version of the content is to use the EntityUtils.toString(HttpEntity)获取内容的字符串版本的更简单方法是使用EntityUtils.toString(HttpEntity)

The exact implementation would look like:确切的实现如下所示:

import org.apache.http.util.EntityUtils;

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) {
            return EntityUtils.toString(entity);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以窥视HttpClient的org.apache.http.conn.EofSensorInputStream? - Is it possible to peek HttpClient's org.apache.http.conn.EofSensorInputStream? Websphere httpclient NoSuchMethodError org.apache.http.conn.Scheme - Websphere httpclient NoSuchMethodError org.apache.http.conn.Scheme HTTPClient“主要” java.lang.NoSuchFieldError:实例位于org.apache.http.conn.ssl.SSLConnectionSocketFactory。 <clinit> - HTTPClient “main” java.lang.NoSuchFieldError: INSTANCE at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit> java.lang.NoClassDefFoundError:org / apache / http / impl / conn / PoolingClientConnectionManager - java.lang.NoClassDefFoundError: org/apache/http/impl/conn/PoolingClientConnectionManager java.lang.NoClassDefFoundError:带有AmazonHttpClient的org / apache / http / conn / SchemePortResolver - java.lang.NoClassDefFoundError: org/apache/http/conn/SchemePortResolver with AmazonHttpClient java.lang.NoClassDefFoundError:org / apache / http / nio / conn / NHttpClientConnectionManager - java.lang.NoClassDefFoundError: org/apache/http/nio/conn/NHttpClientConnectionManager Java Firebase错误“ NoClassDefFoundError:org / apache / http / conn / ssl / StrictHostnameVerifier” - Java Firebase error “NoClassDefFoundError: org/apache/http/conn/ssl/StrictHostnameVerifier” java.lang.NoClassDefFoundError:org / apache / http / conn / params / ConnPerRoute - java.lang.NoClassDefFoundError: org/apache/http/conn/params/ConnPerRoute java.lang.NoClassDefFoundError:org / apache / http / conn / HttpClientConnectionManager - java.lang.NoClassDefFoundError: org/apache/http/conn/HttpClientConnectionManager java.lang.NoClassDefFoundError:org / apache / http / conn / scheme / SchemeSocketFactory - java.lang.NoClassDefFoundError: org/apache/http/conn/scheme/SchemeSocketFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM