HttpClient接口测试中的使用

简介: 作者:嗨刚好遇见你链接:http://www.jianshu.com/p/3f15f1602513TTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一下这三种形式:一.

作者:嗨刚好遇见你
链接:http://www.jianshu.com/p/3f15f1602513
TTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一下这三种形式:

一.GET请求: GET请求时,参数一般是写在链接上的,代码如下:

publicvoidget(String url){

CloseableHttpClient httpClient =null;

HttpGet httpGet =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

httpGet =newHttpGet(url);

httpGet.setConfig(requestConfig);

CloseableHttpResponse response = httpClient.execute(httpGet);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpGet!=null){

httpGet.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

如果想把参数不写在链接上,单独的传进去,则可以这样:

publicvoidget(String url, Map params){

CloseableHttpClient httpClient =null;

HttpGet httpGet =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

String ps ="";

for(String pKey : params.keySet()) {

if(!"".equals(ps)){

ps = ps +"&";

}

ps = pKey+"="+params.get(pKey);

}

if(!"".equals(ps)){

url = url +"?"+ ps;

}

httpGet =newHttpGet(url);

httpGet.setConfig(requestConfig);

CloseableHttpResponse response = httpClient.execute(httpGet);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpGet!=null){

httpGet.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

二. POST请求的表单提交方式,代码如下:

publicvoidpost(String url, Map params){

CloseableHttpClient httpClient =null;

HttpPost httpPost =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

httpPost =newHttpPost(url);

httpPost.setConfig(requestConfig);

List ps =newArrayList();

for(String pKey : params.keySet()) {

ps.add(newBasicNameValuePair(pKey, params.get(pKey)));

}

httpPost.setEntity(newUrlEncodedFormEntity(ps));

CloseableHttpResponse response = httpClient.execute(httpPost);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpPost!=null){

httpPost.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

三. POST请求的RAW参数传递

publicvoidpost(String url, String body){

CloseableHttpClient httpClient =null;

HttpPost httpPost =null;

try{

httpClient = HttpClients.createDefault();

RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();

httpPost =newHttpPost(url);

httpPost.setConfig(requestConfig);

httpPost.setEntity(newStringEntity(body));

CloseableHttpResponse response = httpClient.execute(httpPost);

HttpEntity httpEntity = response.getEntity();

System.out.println(EntityUtils.toString(httpEntity,"utf-8"));

}catch(ClientProtocolException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{

try{

if(httpPost!=null){

httpPost.releaseConnection();

}

if(httpClient!=null){

httpClient.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}
相关文章
|
2天前
Swagger基本使用与RestTemplate发送http接口测试
Swagger基本使用与RestTemplate发送http接口测试
13 1
|
8月前
|
JSON 数据格式
HttpClient远程调用基本使用(详解)
HttpClient远程调用基本使用(详解)
148 0
|
10月前
|
测试技术 网络架构
好用的轻量级http接口测试工具(替代PostMan)
好用的轻量级http接口测试工具(替代PostMan)
|
Web App开发 XML JSON
快速掌握Postman实现接口测试
快速掌握Postman实现接口测试
271 0
|
XML 测试技术 数据格式
http接口自动化测试框架实现
http接口自动化测试框架实现 作者:张元礼 http://blog.csdn.net/vincetest  一、测试需求描述 对服务后台一系列的http接口功能测试。 输入:根据接口描述构造不同的参数输入值 输出:XML文件 eg:http://xxx.com/xxx_product/test/content_book_list.jsp?listid=1   二、实现方法 1、选用Python脚本来驱动测试 2、采用Excel表格管理测试数据,包括用例的管理、测试数据录入、测试结果显示等等,这个需要封装一个Excel的类即可。
1601 0
|
JSON 测试技术 数据格式
SouapUI接口测试之Get和Post请求
马上五一了,首先祝大家五一快乐。 此篇主要介绍SoapUI工具做常用的两种请求接口测试,分别是get请求和post请求 一、GET请求 get请求实例接口为豆瓣的图书接口,可参见豆瓣的图书接口地址:https://developers.douban.com/wiki/?title=book_v2#get_book 豆瓣的图书接口地址 1.在使用SoapUI工具进行接口测试时,三个步骤是必然,那就是先新建SOAP工程,然后在新建的工程中构建测试套件,再然后在构建的测试套件中构建测试用例。
1029 0
|
测试技术
HttpClient接口测试中的使用
作者:嗨刚好遇见你 链接:http://www.jianshu.com/p/3f15f1602513 TTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一下这三种形式: 一.
993 0