servlet之间的数据传递-使用POST方法

相关jar包:

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()));


转载于:https://www.cnblogs.com/dqsweet/archive/2013/04/11/4927736.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值