HttpClient之get请求返回cookie信息+携带这个cookie访问get接口
·
一.moco框架mock接口信息
[
{
"description": "这是一个能返回cookies信息的get请求",
"request": {
"uri": "/getCookies",
"method": "get"
},
"response": {
"cookies": {
"login": "true"
},
"text": "成功获取到cookies信息啦"
}
},
{
"description": "这是一个携带cookie的get请求",
"request": {
"uri": "/get/with/cookie",
"method": "get",
"cookies": {
"login": "true"
}
},
"response": {
"text": "这是一个需要携带cookie的才能访问的get请求"
}
}
]
二.application.properties配置文件信息
test.url=http://localhost:8080
getCookies.uri=/getCookies
test.get.with.cookies=/get/with/cookie
三.编写testNG测试方法
package com.course.httpclient.cookies;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Created with IntelliJ IDEA.
*
* @Auther: 目光定晴天
* @Date: 2021/6/5 11:14
* @Description:
*/
public class MyCookiesForGet {
private String url;
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore cookieStore;
//在测试方法执行前加载配置文件
@BeforeTest
public void beforeTest(){
//如何读取配置文件?---java.util.ResourceBundle,只需要传参为配置文件的前缀即可
bundle = ResourceBundle.getBundle("application", Locale.CHINA);
url = bundle.getString("test.url");
}
//新版httpclient4.5.2
@Test
public void testGetCookies() throws IOException {
//从配置文件拼接测试的URL
String uri;
String testUrl;
uri = bundle.getString("getCookies.uri");
testUrl = this.url + uri;
//获取get方法
HttpGet get = new HttpGet(testUrl);
//创建cookiestore
CookieStore cookieStore = new BasicCookieStore();
//创建CloseableHttpClient对象,同时设置cookiestore
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
//初始化response对象和正文响应字符串为null
CloseableHttpResponse response = null;
String result = null;
//执行get方法
try{
response = client.execute(get);
//为响应数据指定utf-8格式
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
//获取cookiestore,为全局变量cookieStore赋值
this.cookieStore = cookieStore;
//打印输出
List<Cookie> cookieList = this.cookieStore.getCookies();
for (Cookie c:
cookieList) {
System.out.println("cookie name=" + c.getName() + ";cookie value=" + c.getValue());
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//获取cookies信息(HttpClient无法获取,只能通过DefaultHttpClient),
//故需要修改DefaultHttpClient client = new DefaultHttpClient();
// CookieStore cookieStore = client.getCookieStore();
// this.cookieStore = client.getCookieStore();
// List<Cookie> cookieList = cookieStore.getCookies();
// List<Cookie> cookieList = this.cookieStore.getCookies();
// for (Cookie c:
// cookieList) {
// System.out.println("cookie name=" + c.getName() + ";cookie value=" + c.getValue());
// }
}
@Test(dependsOnMethods = {"testGetCookies"})
public void testGetWithCookies() throws IOException {
//1.拼接URL
String uri = bundle.getString("test.get.with.cookies");
String testUrl = this.url + uri;
//2.创建get请求对象 + 创建client对象,设置cookie + 创建response对象
HttpGet get = new HttpGet(testUrl);
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(this.cookieStore).build();
CloseableHttpResponse response = null;
//3.执行get方法获取response,获取状态码,获取响应数据
try {
//执行get方法
response = client.execute(get);
//获取状态码
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("statusCode is :" + statusCode);
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
System.out.println("请求接口成功");
System.out.println("the result is :" + result);
}else {
System.out.println("请求接口失败");
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
response.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四.启动服务,测试运行

更多推荐
所有评论(0)