注意: 本文的分享是基于https://blog.csdn.net/lt326030434/article/details/80453690,感谢原作者的分享~
分享这篇博客是因为原博客里的DefaultHttpClient is deprecated(httpclient4.3以后),所以此处是一个修改过后的方法,供大家参考.
1.首先配置好测试用的mock接口
我们要做的是从/getcookies接口中获取”login”:”true”的cookies信息,然后携带该信息去访问/post/with/cookies2接口
2.全局变量配置url信息
3. 代码实现
package com.gracie.httpclient.cookies;
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.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
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.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
public class MyCookiesForPost {
private String url;
private ResourceBundle bundle;
//用来存储cookies信息的变量
private CookieStore store;
@BeforeTest
public void beforeTest(){
bundle=ResourceBundle.getBundle("application", Locale.CHINA);
url=bundle.getString("test.url");
}
// 获取cookies的信息
@Test
public void testGetcookies() throws IOException {
String result;
//从配置文件中拼接测试的URL
String uri=bundle.getString("getCookies.uri");
String testUrl=this.url+uri;
// 获取cookies信息
this.store= new BasicCookieStore();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();
//测试逻辑
HttpGet httpget=new HttpGet(testUrl);
CloseableHttpResponse response2 = httpclient.execute(httpget);
//打印返回值
result = EntityUtils.toString(response2.getEntity());
System.out.println(result);
//读取cookie信息
List cookielist = store.getCookies();
for(Cookie cookie: cookielist){
String name=cookie.getName();
String value=cookie.getValue();
System.out.println("cookie name =" + name);
System.out.println("Cookie value=" + value);
}
}
// 使用post方法
@Test(dependsOnMethods = {"testGetcookies"})
public void testPostMethod() throws IOException {
String uri=bundle.getString("test.post.with.cookies");
//拼接最终的测试地址
String testUrl=this.url+uri;
//声明一个post的方法
HttpPost httppost=new HttpPost(testUrl);
//添加参数
JSONObject param = new JSONObject();
param.put("name","huhansan");
param.put("age","18");
//设置请求头信息,设置header
httppost.setHeader("content-type", "application/json");
//将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(), "utf-8");
httppost.setEntity(entity);
//声明一个client对象,用来进行方法的执行,并设置cookies信息
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();
//执行post的方法并得到响应结果
CloseableHttpResponse response3 = httpclient.execute(httppost);
//就是判断返回结果是否符合预期
int statusCode = response3.getStatusLine().getStatusCode();
System.out.println("statusCode = "+ statusCode);
String result=EntityUtils.toString(response3.getEntity(),"utf-8");;
if (statusCode==200){
System.out.println(result);
} else {
System.out.println("访问/get/with/cookies接口失败");
}
//将返回的响应结果字符串转化为json对象
JSONObject resultjson = new JSONObject(result);
//获取到结果值
String success = (String) resultjson.get("huhansan");
System.out.println(success);
Assert.assertEquals("success", success);
}
}
4. 获得结果