本文共 4554 字,大约阅读时间需要 15 分钟。
consumer | provider |
---|---|
发送请求 | 接收请求并设置cookie |
接收响应并再次发送请求 | 查看cookie是否有 |
接收响应并再次发送请求 | 环境出现变化,对cookie中的内容进行修改并返回 |
接收响应并再次发送请求 | 获取到新的cookie中的值 |
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/