@Override public void afterPropertiesSet() throws Exception { cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider> create().register("easy", new CookieSpecProvider() { public CookieSpec create(HttpContext context) { return new DefaultCookieSpec() { @Override public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException { } }; } }).build(); requestConfig = RequestConfig.custom().setCookieSpec("easy") .setConnectionRequestTimeout(propertyConfigurer.getIntValue("connection.request.timeout")) .setSocketTimeout(propertyConfigurer.getIntValue("socket_timeout")) .setConnectTimeout(propertyConfigurer.getIntValue("connection_timeout")).build(); }
@Override public CookieSpec create(HttpContext context) { String[] datePatterns = new String[] { DateUtils.PATTERN_RFC1123, DATE_PATTERN_NETSCAPE1,//default Netscape date pattern }; return new DefaultCookieSpec(datePatterns, false) { @Override public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException { //do nothing } }; }
/** * 获取当前Http客户端状态中的Cookie * * @param domain * 作用域 * @param port * 端口 传null 默认80 * @param path * Cookie路径 传null 默认"/" * @param useSecure * Cookie是否采用安全机制 传null 默认false * @return */ public Map<String, Cookie> getCookie(String domain, Integer port, String path, Boolean useSecure) { if (domain == null) { return null; } if (port == null) { port = 80; } if (path == null) { path = "/"; } if (useSecure == null) { useSecure = false; } List<Cookie> cookies = cookieStore.getCookies(); if (cookies == null || cookies.isEmpty()) { return null; } CookieOrigin origin = new CookieOrigin(domain, port, path, useSecure); DefaultCookieSpec cookieSpec = new DefaultCookieSpec(); Map<String, Cookie> retVal = new HashMap<String, Cookie>(); for (Cookie cookie : cookies) { if (cookieSpec.match(cookie, origin)) { retVal.put(cookie.getName(), cookie); } } return retVal; }
public RestResponseEntity build() { if (resultString == null && result == null) { throw new IllegalArgumentException("Either resultString or result must be set"); } if (!cookies.isEmpty()) { DefaultCookieSpec defaultCookieSpec = new DefaultCookieSpec(); defaultCookieSpec.formatCookies(cookies) .forEach(header -> headers.put(header.getName(), header.getValue())); } return new RestResponseEntity(statusCode, result, resultString, headers); }
@Before public void setUp() throws Exception { this.cookieOrigin = new CookieOrigin("localhost", 80, "/", false); this.cookieSpec = new DefaultCookieSpec(); this.cookieStore = new BasicCookieStore(); }