数据准备

在本机或者远端机器安装部署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接口的更多相关文章

  1. HttpClient登陆后获取并携带cookies发起请求

    最近项目中,用到了登陆后获取并携带cookies发起请求的业务场景,现总结写出来备忘一下. 1.定义存取cookies信息的全局变量 public class HttpUtil { /** * 用来存 ...

  2. ReactNative 当前url和cookies的获取

    前面大概介绍了react-native的运行helloword级别的入门,所以之后简单的东西就不写了,毕竟官网上都能够找到. reactnative官网:https://facebook.github ...

  3. Django 设置cookies与获取cookies.

    在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...

  4. Python + request接口测试中Cookie和Session的获取和使用

    Cookie和Session的简单理解  由于Http协议是无状态的,所以产生了cookie和session进行状态的管理. 从哪里来,在哪里,到哪里去: --> Cookie是由服务端生成,存 ...

  5. jquery 获取url携带的参数

    url= "/page/employee/employeeUpdate.html?id="+data.id 获取 url携带的参数 -> $.getUrlParam = fu ...

  6. Android WebView访问网站携带登录认证Cookies和动态自定义的cookies

    最近项目几个页面要复用微信程序的网页.但是需要调用微网站登录接口,返回Cookies,webview访问需要的网页的时候携带. 并且还需要几个其他的动态改变的cookie,目的是根据这几个动态自定义c ...

  7. selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码

    目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...

  8. 答:SQLServer DBA 三十问之六:Job信息我们可以通过哪些表获取;系统正在运行的语句可以通过哪些视图获取;如何获取某个T-SQL语句的IO、Time等信息;

    6. Job信息我们可以通过哪些表获取:系统正在运行的语句可以通过哪些视图获取:如何获取某个T-SQL语句的IO.Time等信息: 我的MSDB数据库中有全部的表: sys.all_columns,s ...

  9. bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能

    xmlrpc .  https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很 ...

随机推荐

  1. C运行时库函数和API函数的区别和联系

    C运行时库函数 C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.    API函数 API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函 ...

  2. shell去掉最后一个字符

    实测过第一种写法,可正常删除 sed 's/.$//' awk '{sub(/.$/,"")}1' awk '{printf $0"\b \n"}' ufile ...

  3. hdu 1548 A strange lift(迪杰斯特拉,邻接表)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. uva 11275 3D Triangles (3D-Geometry)

    uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...

  5. C# TransactionScope 事务类

    微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看Tran ...

  6. 2013年NOIP普及组复赛题解

    题目涉及算法: 计数问题:枚举: 表达式求值:栈: 小朋友的数字:动态规划: 车站分级:最长路. 计数问题 题目链接:https://www.luogu.org/problem/P1980 因为数据量 ...

  7. springmvc web.xml和application.xml配置详情(附:完整版pom.xml)

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  8. 原生Js 实现等比缩放页面

    针对1920*1080 分配率缩放 window.addEventListener('load', adaptation); window.addEventListener('resize', ada ...

  9. Argus--[优先队列]

    Description A data stream is a real-time, continuous, ordered sequence of items. Some examples inclu ...

  10. java 内省综合案例和Beanutils工具包

    演示用eclipse自动生成 ReflectPoint类的setter和getter方法. 直接new一个PropertyDescriptor对象的方式来让大家了解JavaBean API的价值,先用 ...