CloseableHttpClient学习笔记(获取cookies+携带获取的cookies访问get接口)

注意: 本文的分享是基于https://blog.csdn.net/lt326030434/article/details/80449856,感谢原作者的分享~
分享这篇博客是因为原博客里的DefaultHttpClient is deprecated(httpclient4.3以后),所以此处是一个修改过后的方法,供大家参考.

1.首先配置好测试用的mock接口
在这里插入图片描述我们要做的是从/getcookies接口中获取”login”:”true”的cookies信息,然后携带该信息去访问/get/with/cookies接口

2.全局变量配置url信息
在这里插入图片描述
3. 实现代码

package com.gracie.httpclient.cookies;

import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
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 testGetCoookies() throws IOException{

    String result;

    //从配置文件中拼接测试的URL
    String uri=bundle.getString("getCookies.uri");
    String testUrl=this.url+uri;

    // 获取cookies信息
    this.store= new BasicCookieStore();
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(store).build();

    //测试逻辑
    HttpGet httpget=new HttpGet(testUrl);
    CloseableHttpResponse response2 = httpclient.execute(httpget);

    //打印返回值
    result = EntityUtils.toString(response2.getEntity());
    System.out.println(result);

    //读取cookie信息
    List<Cookie> cookielist = store.getCookies();
    for(Cookie cookie: cookielist){
        String name=cookie.getName();
        String value=cookie.getValue();
        System.out.println("cookie name =" + name);
        System.out.println("Cookie name=" + value);
    }



}

@Test(dependsOnMethods = {"testGetCoookies"})
public void testGetwithCookies() throws IOException {

    String uri=bundle.getString("test.get.with.cookies");
    String testUrl=this.url+uri;


    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(this.store).build();

    HttpGet httpget=new HttpGet(testUrl);

    CloseableHttpResponse response3 = httpclient.execute(httpget);
    int statusCode = response3.getStatusLine().getStatusCode();
    System.out.println("statusCode = "+ statusCode);

    if (statusCode==200){
        String result = EntityUtils.toString(response3.getEntity(),"utf-8"); System.out.println(result);
    } else {
        System.out.println("访问/get/with/cookies接口失败");
    }




}

}

  1. 测试结果

在这里插入图片描述5. 如果cookie的value变一下,变成false,得到的结果将是400:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值