本章主要介绍各种请求的编写,及测试类测试方法
一、各种请求的方法编写
package com.qa.restclient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.qa.utils.fatjson.FastjsonUtils;
/*
* 1.实现get请求方法及带请求头信息,POST请求方法,PUT方法,DELETE方法
* 2.一个是带请求头信息的POST请求方法
* 3.获取响应状态码
* 4.json内容解析
*/
public class RestClient {
private static final Logger log = LoggerFactory.getLogger(RestClient.class);
// get请求方法
public static CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
return get(url, null);
}
// Get 请求方法(带请求头信息)
public static CloseableHttpResponse get(String url, Map<String, String> headers)
throws ClientProtocolException, IOException {
// 创建一个可关闭的HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建一个HttpGet的请求对象
HttpGet httpget = new HttpGet(url);
// 加载请求头到httpget对象
if (headers != null && headers.size() > 0) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpget.addHeader(entry.getKey(), entry.getValue());
}
}
// 执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
CloseableHttpResponse httpResponse = httpclient.execute(httpget);
return httpResponse;
}
//POST方法(如果不需要header可传入null),提交form表单(默认application/x-www-form-urlencoded)
public static CloseableHttpResponse postForm(String url, Map<String, String> params, Map<String, String> headers)
throws ClientProtocolException, IOException {
// 设置请求头数据传输格式,使用表单提交的方式,postman中有写
//headers.put("Content-Type","application/x-www-form-urlencoded");
return post(url, null, params, headers);
}
// POST方法,发送json格式(如果不需要header可传入null)
public static CloseableHttpResponse postJson(String url, String jsonString, Map<String, String> headers)
throws ClientProtocolException, IOException {
//准备请求头信息
headers.put("Content-Type", "application/json");//postman中有写
return post(url, jsonString, null, headers);
}
// POST方法
static CloseableHttpResponse post(String url, String jsonString, Map<String, String> params, Map<String, String> headers)
throws ClientProtocolException, IOException {
// 创建一个可关闭的HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建一个HttpPost的请求对象
HttpPost httppost = new HttpPost(url);
// 构造请求体,创建参数队列
if(jsonString != null && !"".equals(jsonString)) {
httppost.setEntity(new St