HttpEntity的使用

HttpEntity实体即可以使流也可以使字符串形式。

具体有什么用法看他的方法解释:

  1. package com.scl.base;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.UnsupportedEncodingException;  
  5.   
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.ParseException;  
  8. import org.apache.http.entity.StringEntity;  
  9. import org.apache.http.util.EntityUtils;  
  10.   
  11. public class HttpClientDemo06 {  
  12.   
  13.     /**  
  14.      * @param args  
  15.      */  
  16.     public static void main(String[] args) {  
  17.         try {  
  18.             HttpEntity entity = new StringEntity("这一个字符串实体", "UTF-8");  
  19.             //内容类型  
  20.             System.out.println(entity.getContentType());  
  21.             //内容的编码格式  
  22.             System.out.println(entity.getContentEncoding());  
  23.             //内容的长度  
  24.             System.out.println(entity.getContentLength());  
  25.             //把内容转成字符串  
  26.             System.out.println(EntityUtils.toString(entity));  
  27.             //内容转成字节数组  
  28.             System.out.println(EntityUtils.toByteArray(entity).length);  
  29.             //还有个直接获得流  
  30.             //entity.getContent();  
  31.         } catch (UnsupportedEncodingException e) {  
  32.             throw new RuntimeException(e);  
  33.         } catch (ParseException e) {  
  34.         } catch (IOException e) {  
  35.         }  
  36.           
  37.           
  38.     }  
  39.   
  40. }  
package com.scl.base;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

public class HttpClientDemo06 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			HttpEntity entity = new StringEntity("这一个字符串实体", "UTF-8");
			//内容类型
			System.out.println(entity.getContentType());
			//内容的编码格式
			System.out.println(entity.getContentEncoding());
			//内容的长度
			System.out.println(entity.getContentLength());
			//把内容转成字符串
			System.out.println(EntityUtils.toString(entity));
			//内容转成字节数组
			System.out.println(EntityUtils.toByteArray(entity).length);
			//还有个直接获得流
			//entity.getContent();
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		} catch (ParseException e) {
		} catch (IOException e) {
		}
		
		
	}

}

对于实体的资源使用完之后要适当的回收资源,特别是对于流实体:例子代码如下

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

 

原文链接:http://www.open-open.com/lib/view/open1326376799874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值