zoukankan      html  css  js  c++  java
  • http 带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 package com.course.httpclient.cookies;
     2 
     3 import org.apache.http.HttpResponse;
     4 import org.apache.http.client.CookieStore;
     5 import org.apache.http.client.HttpClient;
     6 import org.apache.http.client.methods.HttpGet;
     7 import org.apache.http.cookie.Cookie;
     8 import org.apache.http.impl.client.DefaultHttpClient;
     9 import org.apache.http.util.EntityUtils;
    10 import org.testng.annotations.BeforeTest;
    11 import org.testng.annotations.Test;
    12 
    13 import java.io.IOException;
    14 import java.util.List;
    15 import java.util.Locale;
    16 import java.util.ResourceBundle;
    17 
    18 public class MyCookieForGet {
    19 
    20     private String url;
    21     private ResourceBundle bundle;//用于读取配置文件
    22     private CookieStore store;//用于存储cookies信息
    23 
    24     @BeforeTest
    25     public void beforeTest() {
    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() throws IOException {
    35 
    36         String result;
    37         String uri = bundle.getString("getCookies.uri");
    38         //以上代码是获取配置文件中的getCookies.uri对应的路径
    39         String testurl = this.url + uri;
    40         HttpGet get = new HttpGet(testurl);
    41         System.out.println("这是testurl的地址" + testurl);
    42 //        HttpClient client = new DefaultHttpClient(); HttpClient无法获取cookie信息
    43         DefaultHttpClient client = new DefaultHttpClient();
    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() throws IOException {
    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 = new HttpGet(testurl);
    72         DefaultHttpClient client = new DefaultHttpClient();
    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 ]
  • 相关阅读:
    FFF NOJ 2073 裸KM算法
    FFF NOJ 2073 裸KM算法
    FFF NOJ 2073 裸KM算法
    【HDOJ】3242 List Operations
    【HDOJ】2319 Card Trick
    【HDOJ】1760 A New Tetris Game
    【HDOJ】1525 Euclid's Game
    【HDOJ】2217 Visit
    【HDOJ】2144 Evolution
    【HDOJ】1987 Decoding
  • 原文地址:https://www.cnblogs.com/linxinmeng/p/12615254.html
Copyright © 2011-2022 走看看