zoukankan      html  css  js  c++  java
  • 包含cookies的httpget请求

    1、测试类

    package httpclient;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.CookieStore;
    import org.apache.http.client.HttpClient;
    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");
    
    
        }
        //没有cookie
        @Test
        public void testGetCookies() throws IOException {
            String result;
            //从配置文件中拼接测试url
            String uri = bundle.getString("getCookees.uri");
            String testUrl = this.url+uri;
            HttpGet get = new HttpGet(testUrl);
            HttpClient client = new DefaultHttpClient();
            HttpResponse httpResponse= client.execute(get);
            result = EntityUtils.toString(httpResponse.getEntity());
            System.out.println(result);
        }
        //获取cookie
        @Test
        public void testGetCookies1() throws IOException {
            String result;
            //从配置文件中拼接测试url
            String uri = bundle.getString("getCookees.uri");
            String testUrl = this.url+uri;
            HttpGet get = new HttpGet(testUrl);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpResponse httpResponse= client.execute(get);
            result = EntityUtils.toString(httpResponse.getEntity());
            System.out.println(result);
    
            //获取cookie信息
            this.store  = client.getCookieStore();
            List<Cookie> cookieList = store.getCookies();
            for (Cookie cookie:cookieList
                 ) {
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println("cookie name"+name+"cookie value"+value);
            }
        }
        @Test(dependsOnMethods = "testGetCookies1")
        public void testGetWithCookies() throws IOException {
            String uri = bundle.getString("test.get.with.cookies");
            String testUrl = this.url+uri;
            HttpGet get = new HttpGet(testUrl);
            DefaultHttpClient client = new DefaultHttpClient();
            //设置cookies信息
            client.setCookieStore(this.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());
                System.out.println(result);
            }
    
        }
    }
    测试类

    2、配置文件:sources/application.properties

    test.url=http://127.0.0.1:8888
    dev.url=http://127.0.0.1:8888
    getCookees.uri=/getCookies
    test.get.with.cookies=/get/with/cookies
    test.post.with.cookies=/post/with/cookies
    login=/login
    application.properties

    3、Moco的mock文件

    [
      {
        "description":"这是一个会返回cookies信息的get请求",
        "request":{
          "uri":"/getCookies",
          "method":"get"
        },
        "response":{
          "cookies":{
            "login":"true"
          },
          "text":"恭喜你获得cookies信息成功"
        }
    
    
      },
    
    
      {
        "description":"这是一个带cookies信息的get请求",
        "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":"wangkc",
            "age":"18"
          }
        },
        "response":{
          "status":200,
          "json":{
            "huhansan":"success",
            "status":"1"
          }
        }
      }
    
    ]
    mock文件

    4、mock启动命令:java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c  startupWithCoolies.json

  • 相关阅读:
    012 spring retry重试原理的解析
    011 @Retryable的使用
    010 @ControllerAdvice
    009 SpringBoot+Swagger的使用
    008 @Import作用
    007 SpringBoot的@EnableAutoConfiguration注解
    001 品牌管理案例
    000 vue各种基本指令
    013 JS
    002 docker基本的命令
  • 原文地址:https://www.cnblogs.com/wangkc/p/10851384.html
Copyright © 2011-2022 走看看