zoukankan      html  css  js  c++  java
  • 测试框架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);
            }
        }
    
    }
  • 相关阅读:
    手写堆排序和归并排序
    海量数据处理
    HDU 1532 --&&-- POJ1273 dinic 算法
    POJ 3159 最短路 SPFA
    POJ 1459 网络流 EK算法
    8.14比赛j题 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#overview
    单链表---邻接表
    poj 1273 ---&&--- hdu 1532 最大流模板
    HDU 2603 二分匹配
    UVA 796 连通图求桥
  • 原文地址:https://www.cnblogs.com/peiminer/p/9663583.html
Copyright © 2011-2022 走看看