下面列出了怎么用org.apache.http.cookie.CookieSpec的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException,
IOException {
BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
cookie.setVersion(0);
cookie.setPath("/");
cookie.setDomain(jetty.getBaseUrl().getHost());
CookieStore cookieStore = new BasicCookieStore();
CookieSpec cookieSpec = new SolrPortAwareCookieSpecFactory().create(context);
// CookieSpec cookieSpec = registry.lookup(policy).create(context);
// Add the cookies to the request
List<Header> headers = cookieSpec.formatCookies(Collections.singletonList(cookie));
for (Header header : headers) {
request.addHeader(header);
}
context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}
@Override
public CookieSpec newInstance(final HttpParams params) {
if (params != null) {
String[] patterns = null;
final Collection<?> param = (Collection<?>) params.getParameter(
CookieSpecPNames.DATE_PATTERNS);
if (param != null) {
patterns = new String[param.size()];
patterns = param.toArray(patterns);
}
return new PortAwareCookieSpec(patterns);
} else {
return new PortAwareCookieSpec(null);
}
}
@Override
public CookieSpec create(final HttpContext context) {
// SecurityLevel.SECURITYLEVEL_IE_MEDIUM disables path validation
AbstractCookieSpec cookieSpec = new BrowserCompatSpec(null, SecurityLevel.SECURITYLEVEL_IE_MEDIUM);
cookieSpec.registerAttribHandler(CookieUtil.HTTP_ONLY_ATTR, new HttpOnlyHandler());
return cookieSpec;
}
/**
* Removes ";jsessionid=<id>" from the url, if the session id is also set in "httpResponse".
* <p>
* This methods first looks for the following header :
*
* <pre>
* Set-Cookie: JSESSIONID=
* </pre>
*
* If found and perfectly matches the jsessionid value in url, the complete jsessionid definition is removed from
* the url.
*
* @param uri
* original uri, may contains a jsessionid.
* @param httpResponse
* the response which set the jsessionId
* @return uri, without jsession
*/
public static String removeSessionId(String uri, HttpResponse httpResponse) {
CookieSpec cookieSpec = new DefaultCookieSpec();
// Dummy origin, used only by CookieSpec for setting the domain for the
// cookie but we don't need it
CookieOrigin cookieOrigin = new CookieOrigin("dummy", Http.DEFAULT_HTTP_PORT, "/", false);
Header[] responseHeaders = httpResponse.getHeaders("Set-cookie");
String jsessionid = null;
for (Header header : responseHeaders) {
try {
List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin);
for (Cookie cookie : cookies) {
if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
jsessionid = cookie.getValue();
}
break;
}
} catch (MalformedCookieException ex) {
LOG.warn("Malformed header: " + header.getName() + ": " + header.getValue());
}
if (jsessionid != null) {
break;
}
}
if (jsessionid == null) {
return uri;
}
return UriUtils.removeSessionId(jsessionid, uri);
}
@Override
public CookieSpec create(final HttpContext context) {
return new HtmlUnitBrowserCompatCookieSpec(browserVersion_);
}
@Override
public CookieSpec create(final HttpContext context) {
return this.cookieSpec;
}