http 带cookie值的get请求(关联测试)

摘要:
本文主要包含一下五点。getCookieStore()2、如何发送带cookie信息的get请求。setCookieStore()3、testng依耐测试,带cookie信息的get请求需要依耐于获取cookie信息的请求。@Test4、如何使用moco框架构建带cookie的get请求,和返回cookie信息的get请求。

本文主要包含一下五点。

1、如何get请求如何获取cookie信息。 getCookieStore()

2、如何发送带cookie信息的get 请求。setCookieStore()

3、testng依耐测试,带cookie信息的get请求需要依耐于获取cookie信息的请求。 @Test(dependsOnMethods = {"getTestCookie"})

4、如何使用moco框架构建带cookie的get请求,和返回cookie信息的get请求。

5、如何获取请求返回响应的状态码。response.getStatusLine().getStatusCode()

java代码如下

1 packagecom.course.httpclient.cookies;
2 
3 importorg.apache.http.HttpResponse;
4 importorg.apache.http.client.CookieStore;
5 importorg.apache.http.client.HttpClient;
6 importorg.apache.http.client.methods.HttpGet;
7 importorg.apache.http.cookie.Cookie;
8 importorg.apache.http.impl.client.DefaultHttpClient;
9 importorg.apache.http.util.EntityUtils;
10 importorg.testng.annotations.BeforeTest;
11 importorg.testng.annotations.Test;
12 
13 importjava.io.IOException;
14 importjava.util.List;
15 importjava.util.Locale;
16 importjava.util.ResourceBundle;
17 
18 public classMyCookieForGet {
19 
20     privateString url;
21     private ResourceBundle bundle;//用于读取配置文件
22     private CookieStore store;//用于存储cookies信息
23 
24 @BeforeTest
25     public voidbeforeTest() {
26 
27         bundle = ResourceBundle.getBundle("application", Locale.CHINA);
28         //上行代码用于读取配置文件,baseName和类在同一目录的resource文件中
29         url = bundle.getString("test.url");
30         //上行代码是获取配置文件中的域名
31 }
32 
33 @Test
34     public void getTestCookie() throwsIOException {
35 
36 String result;
37         String uri = bundle.getString("getCookies.uri");
38         //以上代码是获取配置文件中的getCookies.uri对应的路径
39         String testurl = this.url +uri;
40         HttpGet get = newHttpGet(testurl);
41         System.out.println("这是testurl的地址" +testurl);
42 //HttpClient client = new DefaultHttpClient(); HttpClient无法获取cookie信息
43         DefaultHttpClient client = newDefaultHttpClient();
44         //创建HttpClient对象,用于执行get请求
45         HttpResponse response =client.execute(get);
46         System.out.println("这是response的值" +response);
47         result = EntityUtils.toString(response.getEntity(), "utf-8");
48 System.out.println(result);
49         //以下代码是获取cookie信息
50         this.store =client.getCookieStore();
51         List<Cookie> cookkielist =store.getCookies();
52         for(Cookie cookie : cookkielist) {
53             String name =cookie.getName();
54             String value =cookie.getValue();
55             System.out.println("cookie name=" + name + "  cookie value=" +value);
56 }
57 
58 
59 }
60 
61     /**
62 * 以下方法是一个带cookie的get请求,因此需要依耐于方法getTestCookie()获取cookie信息
63      */
64     @Test(dependsOnMethods = {"getTestCookie"})
65     public void testGetWithCookies() throwsIOException {
66 
67 String result;
68         String uri = bundle.getString("testGetWithCookies.uri");
69         //以上代码是获取配置文件中的testGetWithCookies.uri对应的路径,如果和配置文件关联上了,配置文件testGetWithCookies.uri字样会高亮显示
70         String testurl = this.url +uri;
71         HttpGet get = newHttpGet(testurl);
72         DefaultHttpClient client = newDefaultHttpClient();
73         client.setCookieStore(this.store);//设置cookie信息
74         HttpResponse response =client.execute(get);
75         int statusCode = response.getStatusLine().getStatusCode();//以上是获取响应的状态码
76         if (statusCode == 200) {
77             result = EntityUtils.toString(response.getEntity(), "utf-8");
78 System.out.println(result);
79 
80 }
81 
82 }
83 
84 }

接口信息配置文件application.properties如下

1 test.url=http://127.0.0.1:8888
2 getCookies.uri=/getCookies
3 testGetWithCookies.uri=/get/with/cookies

moco模拟接口信息如下

1 [
2 {
3     "description": "这是一个会返回cookies信息的get请求",
4     "request": {
5       "uri": "/getCookies",
6       "method": "get"
7 },
8     "response": {
9       "headers": {
10         "Content-Type": "text/html;charset=gbk"
11 },
12       "cookies": {
13         "login": "true"
14 
15 
16 },
17       "text": "恭喜你获得cookies信息成功"
18 }
19 },
20   {"description": "这个一个带cookie信息的get请求",
21   "request": {
22   "uri": "/get/with/cookies",
23   "method": "get",
24   "cookies": {
25     "login": "true"
26 }
27 },
28   "response": {
29   "headers": {
30     "Content-Type": "text/html;charset=gbk"
31 },
32   "text": "这是一个带cookies的请求响应"
33 }
34 }
35 
36 
37 
38 
39 ]

免责声明:文章转载自《http 带cookie值的get请求(关联测试)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Trim 方法(C#)ESXI虚拟机配置公网IP之后,出现重启机器之后,网络出现问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

umi-request 一个新的请求工具

简单封装 npm install umi-request --save 文档,具体封装可以多看看文档配置 import { extend } from "umi-request"; const request = extend({ prefix: "xxx",//相当于baseurl timeout: 10000, errorHandle...

使用Flash上传应该注意的问题。

          使用Flash上传在IE是没问题的,但是在几乎所有的非IE内核浏览器几乎都会遇到一个问题,那就是处理上传的页面或代码无法获取Cookie。不过有趣的事,获取Session是没有问题的。 之前不知道这个bug,害我反反复复弄了好久。在某篇翻译过来的文档找到以下文字:            Cookies and Flash 在Flas...

Java八股文——网络协议

HTTP协议 一次http请求的过程 用户输入url,浏览器本地解析url,如果在host文件中存有对应ip则访问对应ip,否则将域名交给DNS服务器,DNS服务器返回对应IP地址,应用层向ip地址发送http请求,然后是传输层TCP的三次握手确认连接,第一次是客户端向服务器发送syn,第二次是服务器发送syn和ack到客户端,第三次是客户端发送s...

C# HttpWebRequest请求服务器(Get/Post兼容)

简单示例说明 public static string HttpGet(string url, string data,string Method, int timeOut, Encoding encode, string contentType = "application/x-www-form-urlencoded", CookieContainer...

HttpClient 教程 (A)

前言 超文本传输协议(HTTP)也许是当今互联网上使用的最重要的协议了。Web服务,有网络功能的设备和网络计算的发展,都持续扩展了HTTP协议的角色,超越了用户使用的Web浏览器范畴,同时,也增加了需要HTTP协议支持的应用程序的数量。 尽管java.net包提供了基本通过HTTP访问资源的功能,但它没有提供全面的灵活性和其它很多应用程序需要的功能。Ht...

C#编程中关于数据缓存的经验总结

C#编程中关于数据缓存的经验总结一直以来,我都发现程序的运行速度不够理想。通过查代码,发现程序对数据库的访问非常频繁,而且检索出来的数据量比较大。为了让程序运行快起来,我想对程序采用适当的缓存方法。我在C#尝试了5种方法进行数据缓存,具体如下:(如有遗漏,错误欢迎大家指正,欢迎提建议。)1:Session方法:此方法是针对于每个用户来的,如果用户量比较大,...