使用HttpClient4实现API测试实战(2)——多附件上传

[size=x-large][b]0、特别说明[/b][/size]
1、声明:如需转载,请注明来自 http://cgs1999.iteye.com/;
2、[color=red][b]阅读本文前建议先阅读下面博客:[/b][/color]
[url=http://cgs1999.iteye.com/blog/1608003]使用HttpClient4实现API测试实战(1)[/url]

[size=x-large][b]1、引言[/b][/size]
API测试过程中,有些API接口可能需要上传附件,而且是多个附件,本文主要是解决API测试过程中的多附件上传问题。

当然,你也可以将本文当作[color=red][b]使用HttpClient模拟HTTP实现多附件上传[/b][/color]的文章来阅读。

[size=x-large][b]2、更新测试项目[/b][/size]
[size=large][b]2.1 添加项目依赖[/b][/size]
httpmime-4.2.1.jar


[size=large][b]2.2 修改HttpClient帮助类HttpClientUtil[/b][/size]
添加下面方法

public static String doPostUpload(String url, List<BasicNameValuePair> datas, List<String> files) {
try {
// 组装提交信息
MultipartEntity reqEntity = new MultipartEntity();
for(BasicNameValuePair data : datas) {
reqEntity.addPart(data.getName(), new StringBody(data.getValue(), "text/plain", Charset.forName("UTF-8")));
}
for(String file : files) {
reqEntity.addPart("file", new FileBody(new File(file)));
}
// 设置提交信息
HttpPost httppost = new HttpPost(url);
httppost.setEntity(reqEntity);
HttpResponse httpResponse = httpClient.execute(httppost);

// 若状态码为200 ok
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取出回应字串
String strResult = EntityUtils.toString(httpResponse.getEntity());
System.out.println("doPostJson response[" + url + "]: \n" + strResult);
return strResult;
} else {
System.out.println("doPost Error Response[" + url + "]: \n" + httpResponse.getStatusLine().toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}


[size=large][b]2.3 修改API帮助类ApiUtil[/b][/size]
增加多附件测试方法

// 发布带附件信息
public static boolean uploadMessage(String status, List<String> files) {
return uploadMessage(status, null, files);
}
public static boolean uploadMessage(String status, String groupId, List<String> files) {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(0);
params.add(new BasicNameValuePair("account_token", getToken()));
params.add(new BasicNameValuePair("status", status));
if(groupId!=null) {
params.add(new BasicNameValuePair("group_id", groupId));
}

String xml = HttpClientUtil.doPostUpload(API_URL + "/messages/upload", params, files);
if (!hasText(xml)) {
return false;
}

if (xml.indexOf("errorCode") == -1) {
return true;
} else {
return false;
}
}


[size=large][b]2.4 修改ApiUtil中的测试方法[/b][/size]
修改后的测试代码如下

public static void main(String[] argus) {
login("chengesheng@gmail.com", "password");

List<String> files = new ArrayList<String> (0);
files.add("c:\\myimage.jpg");
files.add("c:\\dulala.txt");
uploadMessage("测试附件和图片上传1", "151", files);
}


[size=large][b]2.5 运行测试[/b][/size]
运行测试代码,带附件信息发布成功;

[size=x-large][b]3、参考资料[/b][/size]
[1] HttpClient中官方范例
    examples\org\apache\http\examples\entity\mime\ClientMultipartFormPost.java
[2] http://evgeny-goldin.com/blog/uploading-files-multipart-post-apache/
[3] http://blog.csdn.net/fengjia10/article/details/7315279
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值