首先是接口配置文件的读取,里面有接口地址
public class TestBase {
public Properties prop;
public static String filePath = "D:"
+ File.separator
+ "\\dnworkspace\\dongnao_1\\src\\main\\java\\com\\httpinterface\\base\\config.properties";
// 写一个构造函数
public TestBase() {
prop = new Properties();
try {
FileInputStream fis = new FileInputStream(filePath);
prop.load(fis);
System.out.println(prop.get("Host"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最重要的是接口请求方法
public class RestClient {
public void get(String url) throws ClientProtocolException, IOException{
//创建一个可关闭的HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个HttpGet的请求对象
System.out.println(url);
HttpGet httpGet = new HttpGet(url);
//执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接受
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
//拿到http相应的状态码
int responseStatusCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("response status code : " + responseStatusCode);
//把相应内容存储在字符串对象
String responseString = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
//创建Json对象昂,把上面的字符串转化成Json对象
JSONObject responseJson = JSON.parseObject(responseString);
System.out.println("respon json from API : " + responseJson);
//获取相应头信息,返回是一个数组
Header[] headerArray = httpResponse.getAllHeaders();
//创建一个map对象,通过key-value的形式保存相应头
Map<String, String> headerMap = new HashMap<String, String>();
//通过for循环把相应头添加到map中
for (Header header : headerArray) {
headerMap.put(header.getName(), header.getValue());
}
//打印map
System.out.println("response headers : " + headerMap);
}
}
然后才是真正的调用方法
public class GetApiTest extends TestBase {
TestBase testBase;
String host;
static String url;
RestClient restClient;
public void setUp(){
testBase = new TestBase();
host = prop.getProperty("Host");
url = host + "/api/users";
}
public void getAPITest() throws ClientProtocolException, IOException{
restClient = new RestClient();
restClient.get(url);
}
public static void main(String[] args) throws ClientProtocolException, IOException {
GetApiTest test = new GetApiTest();
//System.out.println(url);
test.setUp();
test.getAPITest();
}
}
最后的调用可以通过testNG,其实上面的代码我们也可以进行重构,因为上面的get方法里面做的事情实在是太多了,需要发送请求,需要获取响应报文和响应头等信息,有些信息并不是我们想要的,而且他们都有一个共同点就是都是从httpResponse总获取出来的,那么我们就可以重新封装我们的get方法,同时get方法也分两种,一个是需要我们给请求的头部信息,一个是不需要
//1.Get 请求方法
public CloseableHttpResponse getUrl(String url) throws ClientProtocolException, IOException{
//创建一个可关闭的httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个httpget请求对象
HttpGet httpGet = new HttpGet(url);
//执行请求
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
return httpResponse;
}
//2.Get 请求方法(带请求头信息)
public CloseableHttpResponse getUrl(String url, HashMap<String, String> headermap){
//创建一个可关闭的httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个httpget请求
HttpGet httpGet = new HttpGet(url);
//加载请求头部到httpget对象
for (Map.Entry<String, String> entry : headermap.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue());
}
//执行请求
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
return httpResponse;
}
我们利用同样的思想来封装post请求,与get请求不同,post请求对了一个请求体,那么我们在封装post请求方法的时候把请求体做为一个参数即可
//3.POST方法
public CloseableHttpResponse postUrl(String url, String entityString, HashMap<String, String> headermap) throws UnsupportedEncodingException{
//创建一个可关闭的httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个httppost请求
HttpPost httpPost = new HttpPost(url);
//设置payload
httpPost.setEntity(new StringEntity(entityString));
//加载请求头到httppost对象
for (Map.Entry<String, String> entry : headermap.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
//发送post请求
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
return httpResponse;
}
至于怎么去获得这个postbody,方法有很多中,可以直接以数据驱动的方式在excel表格中去获得,可以通过对象去获得,这里我们假设body是一个Users类的对象转换成json格式再转成string类型的字符串
public class Users {
private String name;
private String job;
public Users(){
super();
}
public Users(String name, String job){
super();
this.name = name;
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public String toString() {
return "Users [name=" + name + ", job=" + job + "]";
}
}
加一个这个东西就好了