servlet之间的数据传递-使用POST方法
2013-04-11 15:42
405 查看
相关jar包:
commons-httpclient.jar
用PostMethod 模拟http post请求,需要解决传递字符串,文件等需求。
httpclient对此,提供了对应实现,实现方法关键在:RequestEntity。
示例:
示例中,是传递一个普通字符型参数。
这个方法代替了以前直接设置Request body。
RequestEntity是一个接口,有很多实现:
ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity, MultipartRequestEntity, StringRequestEntity
基本上从名字上就可以直接看出功能,可以从字符串,流,文件,字节数组中产生request body。
还有更复杂的Multipart,就是夹杂文件和普通字段的提交。
示例如下:
commons-httpclient.jar
PostMethod post = new PostMethod(url); RequestEntity entity = new FileRequestEntity(inputFile, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); int result = httpclient.executeMethod(post);
用PostMethod 模拟http post请求,需要解决传递字符串,文件等需求。
httpclient对此,提供了对应实现,实现方法关键在:RequestEntity。
示例:
RequestEntity requestEntity = newStringRequestEntity(text); post.setRequestEntity(requestEntity);
示例中,是传递一个普通字符型参数。
这个方法代替了以前直接设置Request body。
RequestEntity是一个接口,有很多实现:
ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity, MultipartRequestEntity, StringRequestEntity
基本上从名字上就可以直接看出功能,可以从字符串,流,文件,字节数组中产生request body。
还有更复杂的Multipart,就是夹杂文件和普通字段的提交。
示例如下:
Part[] parts = {new StringPart("source", "695132533"), new StringPart("status", URLEncoder.encode(status, "utf-8")), filePart}; postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
相关文章推荐
- servlet之间的数据传递-使用POST方法
- PHP使用$_POST传递数据并检验输出时弹出CSS代码的解决方法
- 在JQuery中可以使用get,post和ajax方法给服务器端传递数据
- 关于使用EL语句来接收servlet给jsp传递及jsp页面之间传递的数据
- C#使用委托(delegate)实现在两个form之间传递数据的方法
- 使用 Post 方法传递数据
- jsp中使用POST的方法在网页之间传递参数的简单方法
- 两个Activity之间的数据传递(使用简单的intent方法)
- JQuery------$.get()和$.post()传递数据的使用方法
- ThinkPHP与Angular使用POST方法传递数据的问题
- Android中在Activity之间传递数据的另一种方法, 使用application context
- Android中在Activity之间传递数据的另一种方法, 使用application context
- Python模仿POST提交HTTP数据及使用Cookie值的方法
- 使用MVC框架中要注意的问题:如何在页面和用户控件之间传递数据
- Activity与fragment之间数据传递方法
- Android使用JNI实现Java与C之间传递数据
- iphone 使用委托(delegate)在不同的窗口之间传递数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- 详细介绍ASP.NET页面间数据传递的使用方法
- 表单post方法传递数据到后端