首页 > Web开发 > 详细

httpclient框架实现接口自动化的思路(-)

时间:2020-04-26 19:25:43      阅读:80      评论:0      收藏:0      [点我收藏+]

1.通过httpclient封装 发送请求的类

package com.test.httprequest.v2;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @version: V1.0
* @author:
* @className: HttpUtil
* @packageName: com.zokoo.httprequest.v2
* @description: 将V1下的post请求和get 请求优化,封装成工具类
* @data:
**/
public class HttpUtil {
/**
* @Author yang sp
* @Description post 请求
* @Date 15:23 2020/3/1
* @Param [url, hashMap]
* @return java.lang.String
**/
public static String postRequest(String url, HashMap<String,String> hashMap){
//设置接口地址(作为参数传入)
//2.设置请求方式
HttpPost httpPost=new HttpPost(url);
//3.设置测试数据(封装成为map,作为参数传入)
String result="";//作用域
try {
//4.将参数封装至请求体,setEntity()的参数为HttpEntity entity对象,所以通过构造方法新建对象
//构造方法的传入参数为List<?extend NameValuePair>
List<BasicNameValuePair> parameter =new ArrayList<BasicNameValuePair>();
//通过循环将参数一个个取出
for (String key: hashMap.keySet()) {
parameter.add(new BasicNameValuePair(key, (String) hashMap.get(key)));
}
//将参数封装至请求体
httpPost.setEntity(new UrlEncodedFormEntity(parameter,"UTF-8"));
//5.设置客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
//6.发送请求
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
//7.从响应结果取值
int statusCode= httpResponse.getStatusLine().getStatusCode();
// System.out.println(statusCode);
result= EntityUtils.toString(httpResponse.getEntity());

} catch (Exception e) {
e.printStackTrace();
}
return result;

}
/**
* @Author yang sp
* @Description GET 请求
* @Date 20:53 2020/3/15
* @Param [url, hashMap]
* @return java.lang.String
**/
public static String getRequest(String url, HashMap<String,String> hashMap){
String result="";//作用域,先声明
//1.设置接口地址(作为参数传入)
//2.设置测试数据(作为参数传入)将参数从map取值出来,拼接在URL后面
for (String key:hashMap.keySet()) {
int mark=1;
if(mark==1){
url+=("?"+key+"="+hashMap.get(key));
}else {
url+=("&"+key+"="+hashMap.get(key));
}
mark++;
}
//3.设置请求方法(get请求是将参数拼接在接口地址后面)
HttpGet httpGet=new HttpGet(url);
//4.创建客户端
CloseableHttpClient httpClient= HttpClients.createDefault();
try {//5.发送请求
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
//6.从响应结果获取值
int statusCode = httpResponse.getStatusLine().getStatusCode();
// System.out.println(statusCode);
result = EntityUtils.toString(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}

public static String doService(String url, String requestMode,HashMap<String,String> hashMap){
String result=null;
if (requestMode.equals("post")){
result=postRequest(url,hashMap);
}else {
result=getRequest(url,hashMap);
}
return result;
}
}
2.使用poi数据驱动从excel 读取数据
(1)创建Case的类,从将excel读取的数据封装至该类中,这样才不会每次的去解析excel
ps:case的成员变量:url method parameter expectedResponseData actualResponseData
也可以将url method 信息分开 放在restful的类中
(2)解析excel,通过反射将excel的解析到Case类中

httpclient框架实现接口自动化的思路(-)

原文:https://www.cnblogs.com/yangxiaobai/p/12781480.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!