情况:使用HttpClient,发送POST请求A,想使用返回的cookie访问其他接口
结果:接口请求成功,返回200code码,结果访问其他接口时依旧需要登录
分析:访问A成功了,应该是cookie的问题,查看返回内容的cookie时,的确 也没有cookie,查看控制台报警告Invalid cookie header: “Set-Cookie: aid=1edc28ef-ff53-42b0-a94b-49b02f06994b; Path=/; Expires=Wed, 30 Sep 2020 09:36:43 GMT”. Invalid expires attribute: Wed, 30 Sep 2020 09:36:43 GMT
问题:cookie丢失了,跟后台警告有关,cookie解析格式的问题
解决方法:
RequestConfig defaultConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();//这个是重点,修改cookie格式
HttpClientContext context = HttpClientContext.create();
Map paramMap =new HashMap<>();
paramMap.put(“t”,System.currentTimeMillis());
paramMap.put(“team_id”,”xxxx”);
paramMap.put(“name”,”xxx@yoyosys.com.cn”);
paramMap.put(“password”,”xxxx”);
paramMap.put(“locale”,”zh-cn”);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost =new HttpPost(“https://xxxx”);
httpPost.setConfig(defaultConfig);//这个是重点,将cookie配置方式放入请求中
httpPost.setHeader(“Content-Type”,”application/json;charset=UTF-8″);
httpPost.setHeader(“Connection”,”keep-alive”);
httpPost.setHeader(“Accept”,”application/json, text/plain, */*”);
httpPost.setHeader(“Accept-Encoding”,”gzip, deflate, br”);
httpPost.setHeader(“Sec-Fetch-Site”,”same-origin”);
httpPost.setHeader(“Sec-Fetch-Mode”,”cors”);
httpPost.setHeader(“Sec-Fetch-Dest”,”empty”);
httpPost.setHeader(“User-Agent”,”Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36″);
String param= JSONUtils.toJSONString(paramMap);
httpPost.setEntity(new StringEntity(param));
CloseableHttpResponse response = httpClient.execute(httpPost,context);
















暂无评论内容