`
iwebcode
  • 浏览: 2071754 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

HttpEntity的使用

 
阅读更多

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

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

  1. packagecom.scl.base;
  2. importjava.io.IOException;
  3. importjava.io.UnsupportedEncodingException;
  4. importorg.apache.http.HttpEntity;
  5. importorg.apache.http.ParseException;
  6. importorg.apache.http.entity.StringEntity;
  7. importorg.apache.http.util.EntityUtils;
  8. publicclassHttpClientDemo06{
  9. /**
  10. *@paramargs
  11. */
  12. publicstaticvoidmain(String[]args){
  13. try{
  14. HttpEntityentity=newStringEntity("这一个字符串实体","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(UnsupportedEncodingExceptione){
  28. thrownewRuntimeException(e);
  29. }catch(ParseExceptione){
  30. }catch(IOExceptione){
  31. }
  32. }
  33. }

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

  1. publicstaticvoidtest()throwsIllegalStateException,IOException{
  2. HttpResponseresponse=null;
  3. HttpEntityentity=response.getEntity();
  4. if(entity!=null){
  5. InputStreamis=entity.getContent();
  6. try{
  7. //做一些操作
  8. }finally{
  9. //最后别忘了关闭应该关闭的资源,适当的释放资源
  10. if(is!=null){
  11. is.close();
  12. }
  13. //这个方法也可以把底层的流给关闭了
  14. EntityUtils.consume(entity);
  15. //下面是这方法的源码
  16. /*publicstaticvoidconsume(finalHttpEntityentity)throwsIOException{
  17. if(entity==null){
  18. return;
  19. }
  20. if(entity.isStreaming()){
  21. InputStreaminstream=entity.getContent();
  22. if(instream!=null){
  23. instream.close();
  24. }
  25. }
  26. }*/
  27. }
  28. }
转自:http://blog.csdn.net/com360/article/details/7645247
分享到:
评论

相关推荐

    springMVC-HttpEntity(ResponseEntity)demo

    本项目“springMVC-HttpEntity(ResponseEntity)demo”是一个实战演示,展示了如何在Spring MVC应用中使用这两个类来实现与前端jQuery的交互。 首先,`HttpEntity`是Spring框架提供的一个接口,它代表了一个HTTP...

    org.apache.http.httpentity jar包-系列jar包

    import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; ...

    org.apache.http.entity.mime和org.apache.http.legacy

    在实际使用中,如果你的项目需要发送复杂的HTTP请求,比如上传文件或处理包含多个部分的数据,`org.apache.http.entity.mime` 将是你的好帮手。而如果你的代码或依赖的其他库依赖于Apache HttpClient的旧API,那么...

    org.apache.http.entity.mime

    HttpEntity entity = builder.build(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity); ``` 这个例子中,`addTextBody`和`addBinaryBody`分别用于添加文本和二进制数据,`ContentType.create...

    httpmime-4.1.2.jar org.apache.http.entity.mime

    - 使用`HttpPost`或`HttpGet`等请求方法,结合`BasicHttpEntityEnclosingRequest`,可以方便地添加或处理MIME实体。 6. **注意事项** - 在Android应用中使用HttpMime时,需要注意版本兼容性问题,因为不同Android...

    org.apache.http jar包下载

    - `HttpEntity`:表示HTTP消息中的实体,可以是请求体或响应体。 - `EntityUtils`:提供了一些实用方法,如读取实体内容、关闭实体等。 在使用Apache HTTP客户端库时,你需要先创建`HttpClient`实例,然后构造`...

    httpcomponents-client-4.5.5-bin和src_20180518.zip

    org.apache.http的相关类,android高版本SDK需要用到,解决'The import org.apache.http.HttpEntity cannot be resolved'>等问题: > ★org.apache....

    httpmime jar包

    import org.apache.http.HttpEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; // 创建MultipartEntityBuilder ...

    groovy http请求

    HttpEntity entity = response.getEntity() if (entity != null) { println EntityUtils.toString(entity) } } finally { response.close() } ``` 4. **使用HTTPBuilder**: Groovy社区维护了一个名为...

    WCF WebHttp REST Entity Service

    7. **Demos**:提供的示例可能包含如何设置和使用WCF WebHttp REST Entity Service的代码和步骤,帮助开发者了解如何实际操作。 在**description.html**和**license.rtf**文件中,可能包含了服务的详细说明和使用...

    Entity Model Studio 2.5.0

    Entity Model Studio 是一款支持面向对象设计方法的建模及代码生成工具。其主要的作用是帮助开发者提高设计开发软件的效率,改善开发体验,更好更快的完成软件开发工作。本产品在软件的开发的几个必要的步骤:静态...

    使用spring restTemplete 调用腾讯接口返回Entity

    HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); // 设置请求头 ResponseEntity<MyResponse> response = restTemplate.postForEntity(url, entity, MyResponse.class); // 发送请求并转换为...

    org.apache.http 依赖包

    3. **HttpEntity**:表示HTTP消息实体,可以是请求或响应中的数据。它可以是文本、二进制数据或流。 4. **BasicNameValuePair和NameValuePair**:用于构建HTTP请求参数,常见于POST请求的表单数据。 5. **...

    entity linking源码

    使用lucene、lingpipe做实体连接(Entity Linking)。 数据来自dbpidia。 原始博客参考: http://blog.csdn.net/mmc2015/article/category/5795687 绝对值得参考。

    MultipartEntityBuilder使用jar包

    HttpEntity entity = builder.build(); ``` 6. **创建`HttpPost`请求**: ```java HttpPost httpPost = new HttpPost("http://your/upload/url"); httpPost.setEntity(entity); ``` 7. **执行请求**: ```...

    Chinese Entity Linking Comprehensive

    created) at http://nlp.cs.rpi.edu/kbp/2014/ This package contains all evaluation and training data developed in support of TAC KBP Chinese Entity Linking during the four years since the task's ...

    EasyUI框架(ajax for WCF和Entity FrameWork4.1)

    Entity Framework(EF)是微软推出的一种ORM(对象关系映射)工具,允许开发人员使用面向对象的编程方式操作数据库,无需关心底层SQL语句。EF 4.1引入了Code First开发模式,使得开发者可以直接从代码定义数据模型,...

    HttpGet的使用

    HttpEntity entity = response.getEntity(); String responseBody = EntityUtils.toString(entity, StandardCharsets.UTF_8); ``` 最后,记得关闭资源,防止内存泄漏: ```java entity.getContent().close(); ...

    org.apache.http jar包

    import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache....

    Entity Framework Core Cookbook - Second Edition 2016

    Entity Framework Core Cookbook - Second Edition by Ricardo Peres English | 9 Nov. 2016 | ISBN: 1785883305 | 340 Pages | MOBI/EPUB/PDF+Code Files | 6.2 MB Entity Framework is a highly recommended ...

Global site tag (gtag.js) - Google Analytics