转载: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. html状态码

    100——客户必须继续发出请求101——客户要求服务器根据请求转换HTTP协议版本 200——交易成功201——提示知道新文件的URL202——接受和处理.但处理未完成203——返回信息不确定或不完整 ...

  2. 阿里云服务器ubuntu安装java运行环境

    服务器 阿里云服务器ubuntu安装java运行环境 转:http://www.codingyun.com/article/45.html 今天来给大家介绍一下在阿里云ubuntu服务器下安装java ...

  3. Topcoder SRM 603 div1题解

    昨天刚打了一场codeforces...困死了...不过赶在睡前终于做完了- 话说这好像是我第一次做250-500-1000的标配耶--- Easy(250pts): 题目大意:有一棵树,一共n个节点 ...

  4. C++封装随笔

    1接口的封装和设计思想入门 接口的封装和设计思想入门 第一套api函数 #ifndef _CLT_SOCKET_H__ #define _CLT_SOCKET_H__ //客户端初始化环境 int c ...

  5. Java笔记(一)

    1. ConcurrentModificationException 在遍历容器的同时修改容器里的成员对象可能会抛出该异常 http://www.blogjava.net/EvanLiu/archiv ...

  6. 报告撰写,linux使用gimp简单做gif动图

    我想把我的系统菜单完整记录下来,方便查看,如果单纯使用文字比较单调,使用屏幕截图,需要依次打开多个图像查看也不是很方便,就想到了使用动画的形式展示.由于本人的系统一直使用Linux系统,为了一张gif ...

  7. off charging mode flow

    /system/core/init/init.cpp ..... ..... ..... int main(int argc, char** argv) { ..... ..... ..... // ...

  8. MVC中AuthorizeAttribute用法并实现授权管理

    1.创建一个类(用来检查用户是否登录和用户权限)代码如下 public class MemberCheckAttribute : AuthorizeAttribute { //AuthorizeAtt ...

  9. jenkins笔记:手动更新插件

    jenkins服务器连不了外网,需要手动更新插件.上https://wiki.jenkins-ci.org搜索相应的插件,下载Latest Release-最新版本的.hpi.在系统管理->插件 ...

  10. Trapping Rain Water (Bar Height) -- LeetCode

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...