博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient往webflux后端发消息,后端环境变化更改cookie,测试新cookie能否传回,在下次访问的时候再带上
阅读量:3700 次
发布时间:2019-05-21

本文共 4554 字,大约阅读时间需要 15 分钟。

测试场景

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

cookie测试

  • 新建了一个springboot的测试项目
package com.example.cookiedemotest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic 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; }}

转载地址:http://xylcn.baihongyu.com/

你可能感兴趣的文章
Springboot项目集成jsp
查看>>
传统项目架构图
查看>>
SpringCloud-----(1)微服务基础概念
查看>>
Maven父工程(统一管理依赖)
查看>>
SpringCloud---(4)Eureka与Zookeeper的区别
查看>>
Springboot项目中slf4j日志的简单使用
查看>>
Docker(6)---安装mysql
查看>>
Docker(7)---自定义镜像并设置镜像挂载
查看>>
Docker(8)---匿名卷(匿名挂载)和命名卷(具名挂载)
查看>>
指针基础概念
查看>>
RabbitMQ中的4种交换机消息发送机制测试
查看>>
SpringBoot项目中集成RabbitMQ
查看>>
Java8新特性---Stream流式编程
查看>>
前端三大件:HTML入门
查看>>
前端三大件:CSS入门
查看>>
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘da
查看>>
求一个关于MyCat的博客名(1)
查看>>
Mycat启动日志报错:XML document structures must start and end within the same entity.
查看>>
div内的元素垂直居中和水平居中
查看>>
Freemarker引入外部Css样式
查看>>