问题描述:在使用配置参数进行登录成功, 第一次发送请求的结果返回之后, 10多分钟后再次发送请求会报未登录错误,正常来说不应该这样,因为目标url的session失效时间比这久

这样设置之后,短时间内可以 , 但当再次发送请求时还是会出现连接断开问题
这样设置之后,短时间内可以 , 但当再次发送请求时还是会出现连接断开问题
因为httpclient是CloseableHttpClient,所以无法用从cookieStore获取
设置httpClient策略 : 设置保持时间 , 避免出现连接断开
CloseableHttpClient hc = HttpClients.custom() .setUserAgent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)") .setDefaultHeaders(defaultHeaders).setRedirectStrategy(new LaxRedirectStrategy()) .setConnectionManager(cm) .setDefaultRequestConfig(requestConfig) .setDefaultCookieStore(cookieStore) //keep-alive时间过期之后,会主动杀掉连接,并且不会通知客户端,导致conn reset,因此在此设置keep-alive时间避免这种情况 .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { return 30 * 1000; } }) .build();
public static Map<String, Object> doHttpPost(String url, InputStream is, String fileId, CloseableHttpClient httpClient) throws ClientProtocolException, IOException, Exception { logger.info("=== begin request, do http post ===>" + url); Map<String, Object> map = new HashMap<String, Object>(); CloseableHttpResponse response = null; HttpEntity entity = null; HttpPost httpPost = null; try { httpPost = new HttpPost(url); HttpEntity reqFile = MultipartEntityBuilder.create() .setContentType(ContentType.MULTIPART_FORM_DATA) .setBoundary("----WebKitFormBoundaryriApLsyI1shPEGEH") .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addBinaryBody("files", is, ContentType.MULTIPART_FORM_DATA, fileId) .build(); httpPost.setEntity(reqFile); response = httpClient.execute(httpPost); logger.info("上传文件的respons:" + response.toString()); entity = response.getEntity(); String content = EntityUtils.toString(entity, "UTF-8"); logger.info("上传文件返回Json信息 : " + content); map.put("content" , content); } catch (Exception e) { logger.info("=== do http post exception ===",e); httpPost.abort(); throw e; } finally { if (entity != null) { try { EntityUtils.consume(entity); } catch (Exception e) { } } } return map; }