转载:http://blog.csdn.net/tmaskboy/article/details/52355591

最近在写SSM创建的Web项目,写到一个对外接口时需要做测试,接受json格式的数据。在线测试需要放公网地址,无奈localhost无法访问,测试工具需要安装,不想折腾,想到写爬虫的时候用到的HttpClient可以发Post请求,于是进行了尝试。

1.编写请求代码 
由于接口接受json类型的数据,因此构造了对应的实体类,然后使用fastjson转为json,加到请求头中。

    String url = "http://localhost:8080/api/hcp/get";
Map<String, Object> map = new HashMap<String, Object>(); //构造参数
map.put("token", "Tq0kzItQdol1pO4T");
String result = APITest.API(url, JSONObject.toJSONString(map)); //使用FastJson转为json格式
System.out.println(result);

2.APITest.Java帮助类

public class APITest {
/**
*
* @param 要请求的接口地址
* @param post参数
* @return 接口返回的数据
* @throws IOException
*/
public static String API(String url,String parameters) throws IOException{
System.out.println("参数:"+parameters);
HttpClient httpclient = new DefaultHttpClient();
//新建Http post请求
HttpPost httppost = new HttpPost(url); //登录链接
httppost.setEntity(new StringEntity(parameters, Charset.forName("UTF-8")));
httppost.addHeader("Content-type","application/json; charset=utf-8");
httppost.setHeader("Accept", "application/json");
//处理请求,得到响应
HttpResponse response = httpclient.execute(httppost); //打印返回的结果
HttpEntity entity = response.getEntity();
// Header[] map = response.getAllHeaders(); StringBuilder result = new StringBuilder();
if (entity != null) {
InputStream instream = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(instream));
String temp = "";
while ((temp = br.readLine()) != null) {
String str = new String(temp.getBytes(), "utf-8");
result.append(str).append("\r\n");
}
}
return result.toString();
}
}

然后就可以运行了。

参数:{"token":"Tq0kzItQdol1pO4T"}
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.BasicClientConnectionManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
{"reason":"Token已过期","error_code":1,"result":null}

使用HttpClient测试SpringMVC的接口的更多相关文章

  1. HttpClient测试框架

    HttpClient是模拟Http协议客户端请求的一种技术,可以发送Get/Post等请求. 所以在学习HttpClient测试框架之前,先来看一下Http协议请求,主要看请求头信息. 如何查看HTT ...

  2. testing - 测试基本使用接口

    testing - 测试基本使用接口 当你写完一个函数,结构体,main之后,你下一步需要的就是测试了.testing包提供了很简单易用的测试包. 写一个基本的测试用例 测试文件的文件名需要以_tes ...

  3. 用Mockito测试SpringMVC+Hibernate

    用Mockito测试SpringMVC+Hibernate 译自:Spring 4 MVC+Hibernate 4+MySQL+Maven integration + Testing example ...

  4. HttpClient 测试web API上传文件实例

    1.使用HttpClient 测试上传文件并且设置header信息: using Lemon.Common; using Newtonsoft.Json; using System; using Sy ...

  5. SoapUI SoapUI测试WebService协议接口简介

    SoapUI测试WebService协议接口简介 by:授客 QQ:1033553122 1. 创建项目,入口:File -> New SOAP Project,或者右键默认项目Project- ...

  6. HttpClient发送get,post接口请求

    HttpClient发送get post接口请求/*  * post  * @param url POST地址 * @param data POST数据NameValuePair[] * @retur ...

  7. 采用jmeter和泛化测试dubbo服务接口

    采用jmeter和泛化测试dubbo服务接口 http://blog.csdn.net/linuu/article/details/54313560

  8. HttpClient结合PostMethod调用接口

    HttpClient结合PostMethod调用接口   解决方法: HttpClient client = new HttpClient(); PostMethod post = new PostM ...

  9. 测试必备:jmeter测试http协议接口的各种传参方式

    测试接口,postman和jmeter是用得最频繁的工具,特别是jmeter,支持很多种协议,且除了测功能,还能做自动化测试和性能测试.下面主要介绍jmeter测试http协议接口的用法,包含get, ...

随机推荐

  1. HDU1556---树状数组 | 线段树 |*

    输入n,紧接n行,每行a,b n个气球,a,b表示从第a到第b个气球涂一次色,输出每个球最终的涂几次色 暴力超时,优化数据结构 1.树状数组 #include<iostream> #inc ...

  2. Spring Cloud配置文件加载优先级简述

    Spring Cloud中配置文件的加载机制与其它的Spring Boot应用存在不一样的地方:如它引入了bootstrap.properties的配置文件,同时也支持从配置中心中加载配置文件等:本文 ...

  3. 51Nod 1317 相似字符串对

    题目链接 分析: 考虑两个串的关系:$A+C=C+B$,我们观察可以发现,$A$和$B$是循环同构的,如果$A=G+H$,那么$B=H+G$,证明略长懒得写了... 我们知道$A$串有$K^N$种,所 ...

  4. sql id 或使用nolock

    qlserver 批量插入记录时,对有标识列的字段要设置 set IDENTITY_INSERT 表名 on,然后再执行插入记录操作;插入完毕后恢复为 off 设置 格式:  set IDENTITY ...

  5. SQL联合查询(内联、左联、右联、全联)语法

    SQL联合查询(内联.左联.右联.全联)语法 概述: 联合查询效率较高,举例子来说明联合查询:内联inner join .左联left outer join .右联right outer join  ...

  6. centos 解决:Another app is currently holding the yum lock; waiting for it to exit

    centos执行yum时出现错误: Loaded plugins: fastestmirror, refresh-packagekit, security Existing lock /var/run ...

  7. hdu 5138(水题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5138 反着来. #include<iostream> #include<cstdi ...

  8. (sql server)玩转-数据库行列转换

    虽然开发过程中没用过行列转换,但是听说面试时常常会遇到这个问题,以前在网上也看到过大神的例子,今天自己仔细的玩了下,希望和大家分享一下了. 注意:列转行的方法可能是我独创的了,呵呵,因为在网上找不到哦 ...

  9. mac-command-line-doing

    创建文件夹 mkdir myDirectory 新建文件 touch a.html 编辑文件 vim a.html 删除文件 rm a.html 删除整个文件夹 rm -rf myDirectory ...

  10. Unique Word Abbreviation -- LeetCode

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...