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

转载:http://cgs1999.iteye.com/blog/1609756

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

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

2、更新测试项目 
2.1 添加项目依赖 

httpmime-4.2.1.jar

2.2 修改HttpClient帮助类HttpClientUtil 
添加下面方法 

    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;
    }

2.3 修改API帮助类ApiUtil 
增加多附件测试方法

    // 发布带附件信息
    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;
        }
    }

2.4 修改ApiUtil中的测试方法 
修改后的测试代码如下

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

2.5 运行测试 
运行测试代码,带附件信息发布成功; 

3、参考资料 
[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 

posted @ 2015-04-28 17:28  feng_2015  阅读(220)  评论(0)    收藏  举报