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

  • 相关阅读:
    详解 CSS 属性
    【技巧】DataGridView,ListView重新绑定时保持上次滚动位置
    c#自动更新+安装程序的制作 (转)
    C#winform程序安装时自动卸载新版本覆盖旧版本
    C# 操作XML文件,用XML文件保存信息
    c# winform 隐藏tabcontrol标签
    datagridview用get,set访问并加锁,可以控制所有使用datagridview的地方都顺序进行访问
    sql 获取当天开始时间 结束时间
    批量导出指定注册表内容
    sql server 保留小数,向上保留指定位数的小数,仅记录,勿看。
  • 原文地址:https://www.cnblogs.com/wangkc/p/10851384.html
Copyright © 2011-2022 走看看