【接口测试】使用httpClient获取cookies+携带获取的cookies访问get接口
数据准备
在本机或者远端机器安装部署moco-runner(参考:https://blog.csdn.net/qq_32706349/article/details/80472445)
这里我们只需要准备Json文件:
[
{
"description":"这是一个获取cookies信息的get请求",
"request":{
"uri":"/getcookies",
"method":"get"
},
"response":{
"cookies":{
"login":"true"
},
"text":"获得cookies信息成功"
}
},
{
"description":"这是一个带cookies信息的get请求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"这是一个需要携带cookies信息才能访问的get请求"
}
}
]
代码实现
import org.apache.http.HttpEntity;
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.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import java.io.IOException;
import java.util.*; public class CookieTest { //用来存储Cookies信息的变量
private CookieStore store; //调用获取cookie信息的get接口
@Test
public void testGetCookies() throws IOException {
//获取body
String result=null;
store = new BasicCookieStore();
CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();
HttpGet get=new HttpGet("http://localhost:30080/getcookies");
CloseableHttpResponse response=client.execute(get);
HttpEntity entity = response.getEntity();
result= EntityUtils.toString(entity,"utf-8");
System.out.println(result); List<Cookie> cookieList=store.getCookies();
for (Cookie cookie:cookieList){
String name =cookie.getName();
String value = cookie.getValue();
System.out.println("访问/getcookies接口成功,cookie name = "+name+", cookie value = "+value);
}
response.close();
client.close();
}
//调用带cookie信息的get接口
//方式一:
//通过创建cookieStore存放cookie,以此传递到httpclient
@Test
public void testGetWithCookies1() throws IOException {
//创建cookieStore存储cookie
CookieStore cookieStore=new BasicCookieStore();
//创建cookie对象
BasicClientCookie cookie=new BasicClientCookie("login","true");
cookie.setDomain("localhost");
cookie.setPath("/");
System.out.println(cookie);
cookieStore.addCookie(cookie); CloseableHttpClient client=HttpClients.custom().setDefaultCookieStore(cookieStore).build();
HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies");
CloseableHttpResponse response=client.execute(get);
HttpEntity entity=response.getEntity();
String result=EntityUtils.toString(entity,"utf-8");
System.out.println(result); response.close();
client.close();
}
//方式二:
@Test
public void testGetWithCookies2() throws IOException {
CloseableHttpClient client= HttpClients.createDefault();
HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies");
get.setHeader("cookie","login=true");
CloseableHttpResponse response=client.execute(get);
HttpEntity entity=response.getEntity();
String result=EntityUtils.toString(entity,"utf-8");
System.out.println(result); response.close();
client.close();
}
//方式三:
//依赖获取cookie接口,store传递cookie
@Test(dependsOnMethods = {"testGetCookies"})
public void testGetWithCookies3() throws IOException {
CloseableHttpClient client=HttpClients.custom().setDefaultCookieStore(this.store).build();
HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies");
CloseableHttpResponse response=client.execute(get);
HttpEntity entity=response.getEntity();
String result=EntityUtils.toString(entity,"utf-8");
System.out.println(result); response.close();
client.close();
}
}
总结一下遇到的问题:
1、网上教程大多是使用旧的DefaultHttpClient实现获取cookie,但是httpclient新版本已经不推荐,这里使用的是CloseableHttpClient通过setDefaultCookieStore()方式传递cookie
旧的实现方式参考:https://blog.csdn.net/lt326030434/article/details/80449856
新的实现方式参考:https://blog.csdn.net/wsrfljygracie/article/details/89181318
2、在cookie传递的使用方面,找了一些资料,不停调试测试,靠着IDEA的提示和资料调通。
使用CookieStore保持会话的使用方法参考:https://www.cnblogs.com/ssgao/p/8829056.html
需要注意:cookie需要设置name、value、domain(这个没有设置会一直调不通)
3、注意moco-runner部署的服务器地址,如果是测试服务器本机就是localhost,如果是远端机器,url需要填写具体的url地址
4、代码实现是比较简单的远端调用接口的形式,没有使用本地resources目录配置下application.properties、foo.json,后续会学习。
另外,个人感觉json放在本地资源文件调用的必要性不是很大,看工具的使用范围,自用部署在服务器即可,也不需要每次修改后提交代码。
转发请说明出处,谢谢。
如果还有其他问题,欢迎交流。
【接口测试】使用httpClient获取cookies+携带获取的cookies访问get接口的更多相关文章
- HttpClient登陆后获取并携带cookies发起请求
最近项目中,用到了登陆后获取并携带cookies发起请求的业务场景,现总结写出来备忘一下. 1.定义存取cookies信息的全局变量 public class HttpUtil { /** * 用来存 ...
- ReactNative 当前url和cookies的获取
前面大概介绍了react-native的运行helloword级别的入门,所以之后简单的东西就不写了,毕竟官网上都能够找到. reactnative官网:https://facebook.github ...
- Django 设置cookies与获取cookies.
在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...
- Python + request接口测试中Cookie和Session的获取和使用
Cookie和Session的简单理解 由于Http协议是无状态的,所以产生了cookie和session进行状态的管理. 从哪里来,在哪里,到哪里去: --> Cookie是由服务端生成,存 ...
- jquery 获取url携带的参数
url= "/page/employee/employeeUpdate.html?id="+data.id 获取 url携带的参数 -> $.getUrlParam = fu ...
- Android WebView访问网站携带登录认证Cookies和动态自定义的cookies
最近项目几个页面要复用微信程序的网页.但是需要调用微网站登录接口,返回Cookies,webview访问需要的网页的时候携带. 并且还需要几个其他的动态改变的cookie,目的是根据这几个动态自定义c ...
- selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码
目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...
- 答:SQLServer DBA 三十问之六:Job信息我们可以通过哪些表获取;系统正在运行的语句可以通过哪些视图获取;如何获取某个T-SQL语句的IO、Time等信息;
6. Job信息我们可以通过哪些表获取:系统正在运行的语句可以通过哪些视图获取:如何获取某个T-SQL语句的IO.Time等信息: 我的MSDB数据库中有全部的表: sys.all_columns,s ...
- bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能
xmlrpc . https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很 ...
随机推荐
- iptables 负裁平衡(Load balancing)
「负戴平衡」的作用是将连線平均分散给一组伺服器,以充分利用资源.最简单的作法是利用「通讯端口转接」技术,使其以循环顺序选择目的地位址. 设定iptables的组态 各家Linux系统的iptables ...
- @NOI模拟2017.06.30 - T3@ Right
目录 @description@ @solution@ @part - 1@ @part - 2@ @accepted code@ @details@ @description@ JOHNKRAM 和 ...
- Bootstrap之Form表单验证神器: BootstrapValidator(转)
前言:做Web开发的我们,表单验证是再常见不过的需求了.友好的错误提示能增加用户体验.博主搜索bootstrap表单验证,搜到的结果大部分都是文中的主题:bootstrapvalidator.今天就来 ...
- H3C 10BASE-T线缆和接口
- redux【react】
首先介绍一下redux就是Flux的一种进阶实现.它是一个应用数据流框架,主要作用应用状态的管理 一.设计思想: (1).web应用就是一个状态机,视图和状态一一对应 (2).所有的状态保存在一个对象 ...
- SSM整合 上传下载之添加商品
上传下载细节: 导入xml配置文件!! Controller中要配置存储路径,调用transferto上传文件 上传图片 要将图片的类设置为 MultipartFile 图片下载: 源码: 页面展示: ...
- 条件随机场(CRF) - 2 - 定义和形式
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/xueyingxue001/article/details/51498968声明: 1,本篇为个人对& ...
- jq制作tab栏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- H3C RIP协议概述
- Lavarel之环境配置 .env
.env 文件位于项目根目录下,作为全局环境配置文件. 1. 配置参数 // 运行环境名称 APP_ENV=local // 调试模式,开发阶段启用,上线状态禁用. APP_DEBUG=true // ...