HttpEntity实体即可以使流也可以使字符串形式。
具体有什么用法看他的方法解释:
-
packagecom.scl.base;
-
-
importjava.io.IOException;
-
importjava.io.UnsupportedEncodingException;
-
-
importorg.apache.http.HttpEntity;
-
importorg.apache.http.ParseException;
-
importorg.apache.http.entity.StringEntity;
-
importorg.apache.http.util.EntityUtils;
-
-
publicclassHttpClientDemo06{
-
-
/**
-
*@paramargs
-
*/
-
publicstaticvoidmain(String[]args){
-
try{
-
HttpEntityentity=newStringEntity("这一个字符串实体","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(UnsupportedEncodingExceptione){
-
thrownewRuntimeException(e);
-
}catch(ParseExceptione){
-
}catch(IOExceptione){
-
}
-
-
-
}
-
-
}
对于实体的资源使用完之后要适当的回收资源,特别是对于流实体:例子代码如下
-
publicstaticvoidtest()throwsIllegalStateException,IOException{
-
HttpResponseresponse=null;
-
HttpEntityentity=response.getEntity();
-
-
if(entity!=null){
-
-
InputStreamis=entity.getContent();
-
try{
-
//做一些操作
-
}finally{
-
//最后别忘了关闭应该关闭的资源,适当的释放资源
-
if(is!=null){
-
is.close();
-
}
-
//这个方法也可以把底层的流给关闭了
-
EntityUtils.consume(entity);
-
//下面是这方法的源码
-
/*publicstaticvoidconsume(finalHttpEntityentity)throwsIOException{
-
if(entity==null){
-
return;
-
}
-
if(entity.isStreaming()){
-
InputStreaminstream=entity.getContent();
-
if(instream!=null){
-
instream.close();
-
}
-
}
-
}*/
-
}
-
-
-
}
转自:http://blog.csdn.net/com360/article/details/7645247
分享到:
相关推荐
本项目“springMVC-HttpEntity(ResponseEntity)demo”是一个实战演示,展示了如何在Spring MVC应用中使用这两个类来实现与前端jQuery的交互。 首先,`HttpEntity`是Spring框架提供的一个接口,它代表了一个HTTP...
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; ...
在实际使用中,如果你的项目需要发送复杂的HTTP请求,比如上传文件或处理包含多个部分的数据,`org.apache.http.entity.mime` 将是你的好帮手。而如果你的代码或依赖的其他库依赖于Apache HttpClient的旧API,那么...
HttpEntity entity = builder.build(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity); ``` 这个例子中,`addTextBody`和`addBinaryBody`分别用于添加文本和二进制数据,`ContentType.create...
- 使用`HttpPost`或`HttpGet`等请求方法,结合`BasicHttpEntityEnclosingRequest`,可以方便地添加或处理MIME实体。 6. **注意事项** - 在Android应用中使用HttpMime时,需要注意版本兼容性问题,因为不同Android...
- `HttpEntity`:表示HTTP消息中的实体,可以是请求体或响应体。 - `EntityUtils`:提供了一些实用方法,如读取实体内容、关闭实体等。 在使用Apache HTTP客户端库时,你需要先创建`HttpClient`实例,然后构造`...
org.apache.http的相关类,android高版本SDK需要用到,解决'The import org.apache.http.HttpEntity cannot be resolved'>等问题: > ★org.apache....
import org.apache.http.HttpEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; // 创建MultipartEntityBuilder ...
HttpEntity entity = response.getEntity() if (entity != null) { println EntityUtils.toString(entity) } } finally { response.close() } ``` 4. **使用HTTPBuilder**: Groovy社区维护了一个名为...
7. **Demos**:提供的示例可能包含如何设置和使用WCF WebHttp REST Entity Service的代码和步骤,帮助开发者了解如何实际操作。 在**description.html**和**license.rtf**文件中,可能包含了服务的详细说明和使用...
Entity Model Studio 是一款支持面向对象设计方法的建模及代码生成工具。其主要的作用是帮助开发者提高设计开发软件的效率,改善开发体验,更好更快的完成软件开发工作。本产品在软件的开发的几个必要的步骤:静态...
HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); // 设置请求头 ResponseEntity<MyResponse> response = restTemplate.postForEntity(url, entity, MyResponse.class); // 发送请求并转换为...
3. **HttpEntity**:表示HTTP消息实体,可以是请求或响应中的数据。它可以是文本、二进制数据或流。 4. **BasicNameValuePair和NameValuePair**:用于构建HTTP请求参数,常见于POST请求的表单数据。 5. **...
使用lucene、lingpipe做实体连接(Entity Linking)。 数据来自dbpidia。 原始博客参考: http://blog.csdn.net/mmc2015/article/category/5795687 绝对值得参考。
HttpEntity entity = builder.build(); ``` 6. **创建`HttpPost`请求**: ```java HttpPost httpPost = new HttpPost("http://your/upload/url"); httpPost.setEntity(entity); ``` 7. **执行请求**: ```...
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 ...
Entity Framework(EF)是微软推出的一种ORM(对象关系映射)工具,允许开发人员使用面向对象的编程方式操作数据库,无需关心底层SQL语句。EF 4.1引入了Code First开发模式,使得开发者可以直接从代码定义数据模型,...
HttpEntity entity = response.getEntity(); String responseBody = EntityUtils.toString(entity, StandardCharsets.UTF_8); ``` 最后,记得关闭资源,防止内存泄漏: ```java entity.getContent().close(); ...
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 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 ...