在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去发送请求的更多相关文章

  1. 测试框架httpclent 4.HttpClient Post方法实现

    startupWithCookies.json [ { "description":"这是一个会返回cookies信息的get请求", "reques ...

  2. Android -- junit测试框架,logcat获取log信息

    1. 相关概念 白盒测试: 知道程序源代码. 根据测试的粒度分为不同的类型   方法测试 function test         单元测试 unit test                 集成 ...

  3. 测试框架httpclent 2.配置优化方法

    优化就是为了使代码看起来更简便,如果代码里面的每一个请求都写一次url,那么整体代码看起来很乱,而且一旦某个服务器的端口号或者域名有变动,那么所有的url都需要改变,成本太大.为了让代码看起来更简便, ...

  4. 测试框架httpclent 1.HttpClient简介及第一个demo

    httpclient就是一个模拟 发送http请求的一个工具. 首先在pom.xml文件里面添加工具类 <dependencies> <dependency> <grou ...

  5. selenium:IE浏览器获取cookie提示Could not retrieve cookies

    from selenium import webdriver url = "https://www.baidu.com" dr = webdriver.Ie() dr.get(ur ...

  6. Mock8 moco框架如何返回一个cookie信息

    还是用之前的startupWithCookies.json这个文件,直接往里面添加上面的一个代码: [ { "description":"这是一个会返回cookies信息 ...

  7. 创建和获取cookie

    创建和获取cookie 制作人:全心全意 cookie:在互联网中,cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器.通过使用cookie可以标识用户身份,记录用户名和密码,跟踪重复用 ...

  8. 模拟用户登录(获取cookie/实例化session)

    第一种方法:通过本地浏览器保存的cookie进行登陆 url1 = 'https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2F ...

  9. C#开发BIMFACE系列24 服务端API之获取模型数据9:获取单个房间信息

    系列目录     [已更新最新开发文章,点击查看详细] 大厦建筑模型中,基本上包含多个楼层,每个楼层包含多个房间等信息.在<C#开发BIMFACE系列21 服务端API之获取模型数据6:获取单模 ...

随机推荐

  1. C++客户端访问WebService VS2008

    VS2008及之后的版本已经不支持使用C++开发WEBService服务了,如果要在VS上开发WEBService,需要使用C#开发语言. 一.gSOAP简介 gSOAP编译工具提供了一个基于SOAP ...

  2. 使用django 中间件在所有请求前执行功能

    django中间是一个轻级,低耦合的插件,用来改变全局的输入和输出. 一 如何使用中间件 定义中间件 注册中间件 # 这是一个中间件代码片段的说明,在各个位置的代码将在何时执行 def simple_ ...

  3. 【Spring】application.xml文件配置

    什么是Spring? Spring是分层的javaEE full-stack(一站式)轻量级开源框架. ---注解配置--针对SSM <?xml version="1.0" ...

  4. Log4j配置文件详解及实例

     1 ) . 配置根 Logger ,其语法为:   log4j.rootLogger = [ level ] , appenderName, appenderName, … 其中, level 是日 ...

  5. .net 调用java service 代理类方法

        通过Svcutil.exe 工具生成代理类调用 1.找到如下地址“C:\Windows\System32\cmd.exe”  命令行工具,右键以管理员身份运行(视系统是否为win7 而定) 2 ...

  6. springMVC框架核心方法调用源码解析

  7. 磁盘缓存--YYCache 设计思路

    为了设计一个比较好的磁盘缓存,我调查了大量的开源库,包括 TMDiskCache.PINDiskCache.SDWebImage.FastImageCache 等,也调查了一些闭源的实现,包括 NSU ...

  8. 基于 WebGL 3D 的 HTML5 档案馆可视化管理系统

    前言 档案管理系统是通过建立统一的标准以规范整个文件管理,包括规范各业务系统的文件管理的完整的档案资源信息共享服务平台,主要实现档案流水化采集功能.为企事业单位的档案现代化管理,提供完整的解决方案,档 ...

  9. 单机部署 ELK

    对于一个体量不大的系统,运行在单机上的 ELK 就足以胜任日志的处理任务了.本文介绍如何在单台服务器上安装并配置 ELK(elalasticsearch + logstash + kibana),并最 ...

  10. C#中字节数组(byte[])和字符串相互转换

    转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string&quo ...