Attempted read from closed stream.

最近项目中用到 HttpClient ,代码如下,我用的是 get的方式:

  public static String  get() throws IOException, InterruptedException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpEntity entity = null;
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet("http://127.0.0.1:2333/personal-api/api/personal/signn/personss?phone=11111111&name=hxy")
            httpget.setHeader("User-Agent","Mozilla/5.0");
            httpget.setHeader("Content-Type","application/json");
            System.out.println("executing request " + httpget.getURI());
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                 entity = response.getEntity();
                System.out.println("--------------------------------------");
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    System.out.println("Response content length: " + entity.getContentLength()); 
                    // 打印响应内容
                    System.out.println("Response content: " + EntityUtils.toString(entity)); //这里要多留意一下,下面会说到
                }
                System.out.println("------------------------------------");
                return EntityUtils.toString(entity); //这是返回值
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
               httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    } 
 //main 方法:
    public static void main(String[] args) throws InterruptedException, IOException {
        String s1 = get();
        System.out.println("打印的是: "+s1);//打印返回值内容
    }

运行 main方法,会出现以下异常:

java.io.IOException: Attempted read from closed stream.
 at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:165)
 at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:135)
 at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
 at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
 at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
 at java.io.InputStreamReader.read(InputStreamReader.java:184)
 at java.io.Reader.read(Reader.java:140)
 at org.apache.http.util.EntityUtils.toString(EntityUtils.java:225)
 at org.apache.http.util.EntityUtils.toString(EntityUtils.java:306)
 at cc.huluwa.windowmain.config.TestHttpClients.get(TestHttpClients.java:105)
 at cc.huluwa.windowmain.config.TestHttpClients.main(TestHttpClients.java:73)

出现这样的错误因为:
EntityUtils.toString(entity);这句导致了后面的写入文件错误.entity所得到的流是不可重复读取的也就是说所得的到实体只能一次消耗完,不能多次读取,所以在执行EntityUtils.toString(entity)后,流就关闭了,就导致后面的读和写显示错误.
解决方法:
System.out.println(“Response content: ” + EntityUtils.toString(entity));这句话注释掉,不要打印在控制台

你可能感兴趣的:(HttpClient)