学习笔记(十五)测试框架HttpClient

第4节 获取cookies信息

 //用来存储cookies信息的变量
 private CookieStore store;

 @Test
    public void testGetCookies() throws IOException {

        String result;
        //从配置文件中拼接url
        String uri = bundle.getString("getCookies.uri");
        String testUrl = this.url+uri;
        HttpGet get = new HttpGet(testUrl);
        //获取DefaultHttpClient请求
        DefaultHttpClient client = new DefaultHttpClient();
        //执行get方法
        HttpResponse response = client.execute(get);

        //拿到响应结果的实体entity
        result  = EntityUtils.toString(response.getEntity(), "utf-8");
        System.out.println("result "+result);

        //获取cookies信息
        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);
        }

    }

 

第5节 携带cookies信息访问get请求

   @Test(dependsOnMethods = {"testGetCookies"})
    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(),"utf-8");
            System.out.println(result);
        }
    }

执行结果:

 

在学习过程中遇到的问题:

在读取配置文件的时候总是提示:

java.util.MissingResourceException: Can't find bundle for base name application, locale zh_CN

搜索了一番都是说路径不对要写绝对路径,但是试了也不行,新建了一个项目后,配置一样路径一样再次测试就成功了,不知道之前是由于什么原因导致的,目前还没有解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值