CloseableHttpClient学习笔记(获取cookies+携带获取的cookies访问get接口)
注意: 本文的分享是基于https://blog.csdn.net/lt326030434/article/details/80449856,感谢原作者的分享~
分享这篇博客是因为原博客里的DefaultHttpClient is deprecated(httpclient4.3以后),所以此处是一个修改过后的方法,供大家参考.
1.首先配置好测试用的mock接口
文章图片
我们要做的是从/getcookies接口中获取”login”:”true”的cookies信息,然后携带该信息去访问/get/with/cookies接口
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.cookie.Cookie;
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.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 MyCookiesForGet {
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”);
}
@Test
public void testGetCoookies() 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=https://www.it610.com/article/cookie.getValue();
System.out.println("cookie name =" + name);
System.out.println("Cookie name=" + value);
}}@Test(dependsOnMethods = {"testGetCoookies"})
public void testGetwithCookies() throws IOException {String uri=bundle.getString("test.get.with.cookies");
String testUrl=this.url+uri;
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();
HttpGet httpget=new HttpGet(testUrl);
CloseableHttpResponse response3 = httpclient.execute(httpget);
int statusCode = response3.getStatusLine().getStatusCode();
System.out.println("statusCode = "+ statusCode);
if (statusCode==200){
String result = EntityUtils.toString(response3.getEntity(),"utf-8");
System.out.println(result);
} else {
System.out.println("访问/get/with/cookies接口失败");
}}
}
- 测试结果
文章图片
5. 如果cookie的value变一下,变成false,得到的结果将是400:
文章图片
文章图片
推荐阅读
- EffectiveObjective-C2.0|EffectiveObjective-C2.0 笔记 - 第二部分
- 由浅入深理解AOP
- 继续努力,自主学习家庭Day135(20181015)
- python学习之|python学习之 实现QQ自动发送消息
- Android中的AES加密-下
- 一起来学习C语言的字符串转换函数
- 定制一套英文学习方案
- 漫画初学者如何学习漫画背景的透视画法(这篇教程请收藏好了!)
- 《深度倾听》第5天──「RIA学习力」便签输出第16期
- 如何更好的去学习