1.首先配置好测试用的mock接口
[
{
"description": "模拟会返回cookies的get请求",
"request": {
"uri": "/getcookies",
"method": "get"
},
"response": {
"cookies": {
"login": "flase"
},
"json": {
"text": "这是一个会返回cookies信息的get请求"
}
}
},
{
"description": "模拟一个带cookies的get请求",
"request": {
"uri": "/get/with/cookies",
"method": "get",
"cookies": {
"login": "true",
"token": "test"
}
},
"response": {
"json": {
"text": "这是一个需要携带cookies信息才能访问的get请求"
}
}
}
]
2.在Terminal里执行命令,启动mock
java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.11.0-standalone.jar http -p 8890 -c startupGet.json
3.在httpclient调用方法里获取cookies信息
/**
* http请求方法的封装
*/
public class RestClient {
CookieStore store;
/**
* 不带请求头的get方法封装
* @param url
* @return 返回响应对象
* @throws ClientProtocolException
* @throws IOException
*/
public CloseableHttpResponse get(String url) throws ClientProtocolException, IOException {
//创建一个可关闭的HttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 设置cookies信息
store = new BasicCookieStore();
httpclient = HttpClients.custom().setDefaultCookieStore(store).build();
//创建一个Httpget的请求对象
HttpGet httpget = new HttpGet(url);
//执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
CloseableHttpResponse httpResponse = httpclient.execute(httpget);
// 读取cookie信息
List<Cookie> cookielist = store.getCookies();
for (Cookie cookie : cookielist) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie:name=" + name + ";value=" + value);
}
return httpResponse;
}
}
4.在httpclient调用方法里携带cookies访问信息
/**
* 不带请求头带cookies信息的get方法封装
* @param url
* @param cookieList,存放多个cookies信息
* @return 返回响应对象
* @throws ClientProtocolException
* @throws IOException
*/
public CloseableHttpResponse get(String url,ArrayList cookieList) throws ClientProtocolException, IOException {
// 设置cookies信息
store = new BasicCookieStore();
for (Object x:
cookieList) {
store.addCookie((Cookie) x);
}
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();
//创建一个Httpget的请求对象
HttpGet httpget = new HttpGet(url);
//执行请求,相当于postman上点击发送按钮,然后赋值给HttpResponse对象接收
CloseableHttpResponse httpResponse = httpclient.execute(httpget);
return httpResponse;
}
5.调用返回cookies信息的接口和需要携带cookies信息才能访问的接口
public class Case2 {
public static void main(String[] args){
@Test
public void test3() throws IOException {
String url;
url = "http://127.0.0.1:8890/getcookies";
RestClient restClient = new RestClient();
CloseableHttpResponse closeableHttpResponse = restClient.get(url);
JSONObject responseJson = restClient.getResponseJson(closeableHttpResponse);
System.out.println("responseJson:" + responseJson);
}
@Test
public void test4() throws IOException {
String url;
url = "http://127.0.0.1:8890/get/with/cookies";
//添加cookies信息
BasicClientCookie cookie1 = new BasicClientCookie("login","true");
cookie1.setVersion(0);
cookie1.setDomain("127.0.0.1");
cookie1.setPath("/get/with/cookies");
//添加cookies信息
BasicClientCookie cookie2 = new BasicClientCookie("token","test");
cookie2.setVersion(0);
cookie2.setDomain("127.0.0.1");
cookie2.setPath("/get/with/cookies");
ArrayList cookieList = new ArrayList();
cookieList.add(cookie1);
cookieList.add(cookie2);
RestClient restClient = new RestClient();
CloseableHttpResponse closeableHttpResponse = restClient.get(url,cookieList);
JSONObject responseJson = restClient.getResponseJson(closeableHttpResponse);
System.out.println("responseJson:" + responseJson);
}
}
执行结果: