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);
}
}
執行結果: