HttpClient往webflux后端发消息,后端环境变化更改cookie,测试新cookie能否传回,在下次访问的时候再带上

测试场景

consumerprovider
发送请求接收请求并设置cookie
接收响应并再次发送请求查看cookie是否有
接收响应并再次发送请求环境出现变化,对cookie中的内容进行修改并返回
接收响应并再次发送请求获取到新的cookie中的值

cookie测试

  • 新建了一个springboot的测试项目
package com.example.cookiedemotest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CookiedemotestApplication {

    public static void main(String[] args) {
        SpringApplication.run(CookiedemotestApplication.class, args);
    }

}

  • 测试函数
package com.example.cookiedemotest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;

import static java.lang.Thread.sleep;

@RunWith(SpringRunner.class)

@SpringBootTest(classes = CookiedemotestApplication.class)
public class cookieTest {


    protected final Logger logger = LoggerFactory.getLogger(cookieTest.class);

    @Test
    public void testCookie(){

        HttpUtils httpUtil = new HttpUtils();
        try {
            String url = "http://xx.xx.xx.xxx:xxx/testCookie";
            for (int i= 0 ; i<= 50; i++){
                String ret =  httpUtil.post(url,"test");
                logger.info("result:"+ret);
            }

           // String ret =  httpUtil.post(url,"test");

        }
        catch (Exception e){
            logger.info("发生了异常:"+e.getMessage());
            return;

        }

        return;
    }

}

  • 工具类

package com.example.cookiedemotest;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.http.client.CookieStore;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;


public class HttpUtils {

    protected final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
    private static CookieStore cookieStore;
    /**
     * 发送 get 请求
     *
     * @param url 请求地址
     * @return 请求结果
     */
    public String get(String url) {
        String result = null;
        CloseableHttpResponse response = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String post(String url, String jsonStr) {
        // 创建httpClient
        //CloseableHttpClient httpClient = HttpClients.createDefault();

        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.setCookieStore(this.cookieStore);

        // 创建post请求方式实例
        HttpPost httpPost = new HttpPost(url);

        // 设置请求头 发送的是json数据格式
        httpPost.setHeader("Content-type", "application/json;charset=utf-8");
        httpPost.setHeader("Connection", "Close");

        // 设置参数---设置消息实体 也就是携带的数据
        StringEntity entity = new StringEntity(jsonStr, Charset.forName("UTF-8"));
        // 设置编码格式
        entity.setContentEncoding("UTF-8");
        // 发送Json格式的数据请求
        entity.setContentType("application/json");
        // 把请求消息实体塞进去
        httpPost.setEntity(entity);

        // 执行http的post请求
        CloseableHttpResponse httpResponse;
        String result = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            Object object = httpResponse.getHeaders("set-cookie").toString();
            logger.info("set-cookie:"+object);
            // 获得cookie并存取
            this.cookieStore = httpClient.getCookieStore();

            result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值