测试框架httpclent 3.获取cookie的信息,然后带cookies去发送请求
在properties文件里面:

startupWithCookies.json
[
{
"description":"这是一个会返回cookies信息的get请求",
"request":{
"uri":"/getCookies",
"method":"get" },
"response":{
"cookies":{
"login":"true"
},
"text":"恭喜获得cookies信息成功"
} }, {
"description":"这是一个带cookies的请求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"这是一个需要携带cookies信息才能访问的get请求"
}
}, {
"description":"这是一个带cookies的post请求",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"huhanshan",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"huhanshan":"success",
"status":"1"
}
}
} ]
进入moco和json文件的所在目录:运行以下命令
java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c startupWithCookies.json
Java文件:
package com.course.httpclient.cookies; import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
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 testGetGookies() throws IOException {
String result;
String uri = bundle.getString("getCookies.uri");
HttpGet get = new HttpGet(this.url + uri);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get); result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result); //获取cookies的信息,因为cookie里面不只是一个,他是一个cookie类型的list
store = client.getCookieStore();
List<Cookie> cookieList = store.getCookies(); for(Cookie cookie : cookieList){
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("name = "+name+",value = "+value);
}
} @Test(dependsOnMethods = "testGetGookies")
public void testGetWithCookies() throws IOException {
String uri = bundle.getString("test.get.with.cookies");
HttpGet get = new HttpGet(this.url + uri);
DefaultHttpClient client = new DefaultHttpClient(); //设置cookies信息
client.setCookieStore(store); HttpResponse response = client.execute(get); //获取响应的状态码
int statusCode = response.getStatusLine().getStatusCode(); System.out.println("statusCode="+statusCode); if(statusCode==200){
String result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
}
} }
测试框架httpclent 3.获取cookie的信息,然后带cookies去发送请求的更多相关文章
- 测试框架httpclent 4.HttpClient Post方法实现
startupWithCookies.json [ { "description":"这是一个会返回cookies信息的get请求", "reques ...
- Android -- junit测试框架,logcat获取log信息
1. 相关概念 白盒测试: 知道程序源代码. 根据测试的粒度分为不同的类型 方法测试 function test 单元测试 unit test 集成 ...
- 测试框架httpclent 2.配置优化方法
优化就是为了使代码看起来更简便,如果代码里面的每一个请求都写一次url,那么整体代码看起来很乱,而且一旦某个服务器的端口号或者域名有变动,那么所有的url都需要改变,成本太大.为了让代码看起来更简便, ...
- 测试框架httpclent 1.HttpClient简介及第一个demo
httpclient就是一个模拟 发送http请求的一个工具. 首先在pom.xml文件里面添加工具类 <dependencies> <dependency> <grou ...
- selenium:IE浏览器获取cookie提示Could not retrieve cookies
from selenium import webdriver url = "https://www.baidu.com" dr = webdriver.Ie() dr.get(ur ...
- Mock8 moco框架如何返回一个cookie信息
还是用之前的startupWithCookies.json这个文件,直接往里面添加上面的一个代码: [ { "description":"这是一个会返回cookies信息 ...
- 创建和获取cookie
创建和获取cookie 制作人:全心全意 cookie:在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用 ...
- 模拟用户登录(获取cookie/实例化session)
第一种方法:通过本地浏览器保存的cookie进行登陆 url1 = 'https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2F ...
- C#开发BIMFACE系列24 服务端API之获取模型数据9:获取单个房间信息
系列目录 [已更新最新开发文章,点击查看详细] 大厦建筑模型中,基本上包含多个楼层,每个楼层包含多个房间等信息.在<C#开发BIMFACE系列21 服务端API之获取模型数据6:获取单模 ...
随机推荐
- po编程——自动化测试面试必问
先来看一个在腾讯课堂首页搜索机构的操作步骤: 1:首先打开腾讯课堂的首页:https://ke.qq.com 2:点击课程或机构的下拉选择图标 3:选择机构 4:在搜索框输入要搜索的机构名称 5:点击 ...
- idea连接服务器上传jar并运行
.打开idea tools-deployment-configuration.. .打开如图 点击 + 号 .如 ...
- SQL Server 数据库部分常用语句小结(二)
9. 查询备份还原数据库的进度. select command ,percent_complete ,est_time_to_go=convert(varchar,(estimated_complet ...
- 搭建MHA时 yum 安装perl模块提示 baseurl 错误
今天在搭建MySQL MHA 安装MHA node所需的perl模块(DBD:mysql)时遇到了一个小的错误,如果思路不对的话,还是产生不少麻烦. 现梳理记录下来. 问题现象 执行的命令 yum ...
- Spring Ioc工作机制 初步
Spring IoC工作原理 Spring 启动时读取应用程序提供的Bean配置信息,并在Spring容器中生成一份相应的Bean配置注册表,然后根据这张注册表实例化Bean,装配好Bean之间的依赖 ...
- VS根据数据库生成实体类
一.在类库项目上添加新项 二. 三.依次填入数据库连接 选择数据库 就可以生成数据库实体
- iOS开发之OC与swift开发混编教程,代理的相互调用,block的实现。OC调用Swift中的代理, OC调用Swift中的Block 闭包
本文章将从两个方向分别介绍 OC 与 swift 混编 1. 第一个方向从 swift工程 中引入 oc类 1. 1 如何在swift的类中使用oc类 1.2 如何在swift中实现oc的代理 ...
- Django 路由层(urlconf)
Django 的路由层(URLconf) URL配置(conf)就像是Django所支撑的网站的目录; 本质就是:URL与调用该URL执行的视图函数的映射表; 通俗的讲:就是用户使用哪个url,URL ...
- 关于使用国内dock仓库,网易、DaoCloud
使用国内docker镜像仓库,大大提高镜像的下载速度,从docker hub下载慢的不要不要的,甚至根本下载不了镜像,在docker for windows 18.06中增加一个配置即可,非常简单,具 ...
- dispatch_barrier_async--屏障是一个同步点
Discussion Calls to this function always return immediately after the block has been submitted and n ...