HttpEntity的使用

  1. package com.scl.base;  
  2. import java.io.IOException;  
  3. import java.io.UnsupportedEncodingException;  
  4. import org.apache.http.HttpEntity;  
  5. import org.apache.http.ParseException;  
  6. import org.apache.http.entity.StringEntity;  
  7. import org.apache.http.util.EntityUtils;  
  8. public class HttpClientDemo06 {  
  9.     /**  
  10.      * @param args  
  11.      */  
  12.     public static void main(String[] args) {  
  13.         try {  
  14.             HttpEntity entity = new StringEntity("这一个字符串实体", "UTF-8");  
  15.             //内容类型  
  16.             System.out.println(entity.getContentType());  
  17.             //内容的编码格式  
  18.             System.out.println(entity.getContentEncoding());  
  19.             //内容的长度  
  20.             System.out.println(entity.getContentLength());  
  21.             //把内容转成字符串  
  22.             System.out.println(EntityUtils.toString(entity));  
  23.             //内容转成字节数组  
  24.             System.out.println(EntityUtils.toByteArray(entity).length);  
  25.             //还有个直接获得流  
  26.             //entity.getContent();  
  27.         } catch (UnsupportedEncodingException e) {  
  28.             throw new RuntimeException(e);  
  29.         } catch (ParseException e) {  
  30.         } catch (IOException e) {  
  31.         }  
  32.           
  33.           
  34.     }  
  35.   
  36. }  
  37. 对于实体的资源使用完之后要适当的回收资源,特别是对于流实体:例子代码如下
    1. public static void test() throws IllegalStateException, IOException{  
    2.         HttpResponse response = null;  
    3.         HttpEntity entity = response.getEntity();  
    4.           
    5.         if(entity!=null){  
    6.                
    7.                 InputStream is = entity.getContent();  
    8.                 try{  
    9.                     //做一些操作  
    10.                 }finally{  
    11.                     //最后别忘了关闭应该关闭的资源,适当的释放资源  
    12.                     if(is != null){  
    13.                         is.close();  
    14.                     }  
    15.                     //这个方法也可以把底层的流给关闭了  
    16.                     EntityUtils.consume(entity);  
    17.                     //下面是这方法的源码  
    18.                     /*public static void consume(final HttpEntity entity) throws IOException {  
    19.                         if (entity == null) {  
    20.                             return;  
    21.                         }  
    22.                         if (entity.isStreaming()) {  
    23.                             InputStream instream = entity.getContent();  
    24.                             if (instream != null) {  
    25.                                 instream.close();  
    26.                             }  
    27.                         }  
    28.                     }*/  
    29.                 }  
    30.                   
    31.                
    32.         }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值