/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/cookie/CookiePolicy.java,v 1.15 2004/09/14 20:11:31 olegk Exp $
* $Revision: 480424 $
* $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.httpclient.cookie;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Cookie management policy class. The cookie policy provides corresponding
* cookie management interfrace for a given type or version of cookie.
* <p>RFC 2109 specification is used per default. Other supported specification
* can be chosen when appropriate or set default when desired
* <p>The following specifications are provided:
* <ul>
* <li><tt>BROWSER_COMPATIBILITY</tt>: compatible with the common cookie
* management practices (even if they are not 100% standards compliant)
* <li><tt>NETSCAPE</tt>: Netscape cookie draft compliant
* <li><tt>RFC_2109</tt>: RFC2109 compliant (default)
* <li><tt>IGNORE_COOKIES</tt>: do not automcatically process cookies
* </ul>
*
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
*
* @since 2.0
*/
public abstract class CookiePolicy {
private static Map SPECS = Collections.synchronizedMap(new HashMap());
/**
* The policy that provides high degree of compatibilty
* with common cookie management of popular HTTP agents.
*
* @since 3.0
*/
public static final String BROWSER_COMPATIBILITY = "compatibility";
/**
* The Netscape cookie draft compliant policy.
*
* @since 3.0
*/
public static final String NETSCAPE = "netscape";
/**
* The RFC 2109 compliant policy.
*
* @since 3.0
*/
public static final String RFC_2109 = "rfc2109";
/**
* The RFC 2965 compliant policy.
*
* @since 3.0
*/
public static final String RFC_2965 = "rfc2965";
/**
* The policy that ignores cookies.
*
* @since 3.0
*/
public static final String IGNORE_COOKIES = "ignoreCookies";
/**
* The default cookie policy.
*
* @since 3.0
*/
public static final String DEFAULT = "default";
static {
CookiePolicy.registerCookieSpec(DEFAULT, RFC2109Spec.class);
CookiePolicy.registerCookieSpec(RFC_2109, RFC2109Spec.class);
CookiePolicy.registerCookieSpec(RFC_2965, RFC2965Spec.class);
CookiePolicy.registerCookieSpec(BROWSER_COMPATIBILITY, CookieSpecBase.class);
CookiePolicy.registerCookieSpec(NETSCAPE, NetscapeDraftSpec.class);
CookiePolicy.registerCookieSpec(IGNORE_COOKIES, IgnoreCookiesSpec.class);
}
/**
* The <tt>COMPATIBILITY</tt> policy provides high compatibilty
* with common cookie management of popular HTTP agents.
*
* @deprecated Use {@link #BROWSER_COMPATIBILITY}
*/
public static final int COMPATIBILITY = 0;
/**
* The <tt>NETSCAPE_DRAFT</tt> Netscape draft compliant policy.
*
* @deprecated Use {@link #NETSCAPE}
*/
public static final int NETSCAPE_DRAFT = 1;
/**
* The <tt>RFC2109</tt> RFC 2109 compliant policy.
*
* @deprecated Use {@link #RFC_2109}
*/
public static final int RFC2109 = 2;
/**
* The <tt>RFC2965</tt> RFC 2965 compliant policy.
*
* @deprecated Use {@link #RFC_2965}
*/
public static final int RFC2965 = 3;
/**
* The default cookie policy.
*
* @deprecated Use {@link #DEFAULT}
*/
private static int defaultPolicy = RFC2109;
/** Log object. */
protected static final Log LOG = LogFactory.getLog(CookiePolicy.class);
/**
* Registers a new {@link CookieSpec cookie specification} with the given identifier.
* If a specification with the given ID already exists it will be overridden.
* This ID is the same one used to retrieve the {@link CookieSpec cookie specification}
* from {@link #getCookieSpec(String)}.
*
* @param id the identifier for this specification
* @param clazz the {@link CookieSpec cookie specification} class to register
*
* @see #getCookieSpec(String)
*
* @since 3.0
*/
public static void registerCookieSpec(final String id, final Class clazz) {
if (id == null) {
throw new IllegalArgumentException("Id may not be null");
}
if (clazz == null) {
throw new IllegalArgumentException("Cookie spec class may not be null");
}
SPECS.put(id.toLowerCase(), clazz);
}
/**
* Unregisters the {@link CookieSpec cookie specification} with the given ID.
*
* @param id the ID of the {@link CookieSpec cookie specification} to unregister
*
* @since 3.0
*/
public static void unregisterCookieSpec(final String id) {
if (id == null) {
throw new IllegalArgumentException("Id may not be null");
}
SPECS.remove(id.toLowerCase());
}
/**
* Gets the {@link CookieSpec cookie specification} with the given ID.
*
* @param id the {@link CookieSpec cookie specification} ID
*
* @return {@link CookieSpec cookie specification}
*
* @throws IllegalStateException if a policy with the ID cannot be found
*
* @since 3.0
*/
public static CookieSpec getCookieSpec(final String id)
throws IllegalStateException {
if (id == null) {
throw new IllegalArgumentException("Id may not be null");
}
Class clazz = (Class)SPECS.get(id.toLowerCase());
if (clazz != null) {
try {
return (CookieSpec)clazz.newInstance();
} catch (Exception e) {
LOG.error("Error initializing cookie spec: " + id, e);
throw new IllegalStateException(id +
" cookie spec implemented by " +
clazz.getName() + " could not be initialized");
}
} else {
throw new IllegalStateException("Unsupported cookie spec " + id);
}
}
/**
* @return default cookie policy
*
* @deprecated Use {@link #getDefaultSpec()}
*
* @see #getDefaultSpec()
*/
public static int getDefaultPolicy() {
return defaultPolicy;
}
/**
* @param policy new default cookie policy
*
* @deprecated Use {@link CookiePolicy#registerCookieSpec(String, Class)}
* @see #DEFAULT
*/
public static void setDefaultPolicy(int policy) {
defaultPolicy = policy;
}
/**
* @param policy cookie policy to get the CookieSpec for
* @return cookie specification interface for the given policy
*
* @deprecated Use {@link CookiePolicy#getCookieSpec(String)}
*/
public static CookieSpec getSpecByPolicy(int policy) {
switch(policy) {
case COMPATIBILITY:
return new CookieSpecBase();
case NETSCAPE_DRAFT:
return new NetscapeDraftSpec();
case RFC2109:
return new RFC2109Spec();
case RFC2965:
return new RFC2965Spec();
default:
return getDefaultSpec();
}
}
/**
* Returns {@link CookieSpec cookie specification} registered as {@link #DEFAULT}.
* If no default {@link CookieSpec cookie specification} has been registered,
* {@link RFC2109Spec RFC2109 specification} is returned.
*
* @return default {@link CookieSpec cookie specification}
*
* @see #DEFAULT
*/
public static CookieSpec getDefaultSpec() {
try {
return getCookieSpec(DEFAULT);
} catch (IllegalStateException e) {
LOG.warn("Default cookie policy is not registered");
return new RFC2109Spec();
}
}
/**
* Gets the CookieSpec for a particular cookie version.
*
* <p>Supported versions:
* <ul>
* <li><tt>version 0</tt> corresponds to the Netscape draft
* <li><tt>version 1</tt> corresponds to the RFC 2109
* <li>Any other cookie value coresponds to the default spec
* <ul>
*
* @param ver the cookie version to get the spec for
* @return cookie specification interface intended for processing
* cookies with the given version
*
* @deprecated Use {@link CookiePolicy#getCookieSpec(String)}
*/
public static CookieSpec getSpecByVersion(int ver) {
switch(ver) {
case 0:
return new NetscapeDraftSpec();
case 1:
return new RFC2109Spec();
default:
return getDefaultSpec();
}
}
/**
* @return cookie specification interface that provides high compatibilty
* with common cookie management of popular HTTP agents
*
* @deprecated Use {@link CookiePolicy#getCookieSpec(String)}
*/
public static CookieSpec getCompatibilitySpec() {
return getSpecByPolicy(COMPATIBILITY);
}
/**
* Obtains the currently registered cookie policy names.
*
* Note that the DEFAULT policy (if present) is likely to be the same
* as one of the other policies, but does not have to be.
*
* @return array of registered cookie policy names
*
* @since 3.1
*/
public static String[] getRegisteredCookieSpecs(){
return (String[]) SPECS.keySet().toArray(new String [SPECS.size()]);
}
}
...
分享到:
相关推荐
4. **Cookie 管理**:内置的 Cookie 管理器可以自动处理服务器返回的 Cookie,维持会话状态。 5. **重试策略**:HttpClient 允许开发者配置重试策略,当遇到网络不稳定或临时错误时,可以自动重试请求。 6. **认证...
- **状态管理参数**:控制Cookie和其他状态信息的处理方式。 - **规格注册**:注册自定义的Cookie规格。 - **选择Cookie策略**:根据应用场景选择合适的Cookie策略。 - **自定义Cookie策略**:实现自己的Cookie...
认证方案定义了多种认证机制,使得HttpClient可以支持包括NTLM、SPNEGO和Kerberos在内的多种认证方式。 #### 凭证提供者 凭证提供者允许用户自定义凭证获取的逻辑,以满足特定的认证需求。 #### 认证数据缓存 认证...
以上内容只是HttpClient使用和理解的一部分,实际应用中还会涉及更多的高级特性,如Cookie管理、代理设置、性能优化等。HttpClient是一个功能丰富的库,对于任何需要与HTTP服务器交互的Java应用来说,都是一个强大的...
它支持HTTPS、Cookie管理、重定向处理、自定义头信息等功能,使得开发者能够方便地与Web服务器进行交互。HttpClient的主要优点在于其灵活性和可定制性,允许开发者根据实际需求调整请求行为和响应处理。 使用...
通过`HttpClientBuilder`,开发者可以定制各种设置,如连接管理、重试策略、超时、缓存等。例如,你可以创建一个具有连接池的`HttpClient`实例,以便更有效地管理和复用TCP连接,减少网络延迟。 `HttpGet`、`...
它包含多个模块,包括 HttpCore、HttpClient、HttpAuth、HttpCookie 等,每个模块都提供了不同的功能和实现方式。 HttpCore 模块是 HttpComponents 库的核心部分,实现了大部分 HTTP 协议的基础部分。它提供了公用 ...
HttpClient提供了更高级的功能,如多线程、重试策略和Cookie管理,适合复杂的网络请求场景。 总结来说,Java Web Servlet服务后端框架涉及了Web应用开发的多个方面:使用框架构建后端服务,通过JSON进行数据交换,...
- **Cookie管理**:对于需要登录验证的网站,SmartSpider支持Cookie管理,保持会话状态。 - **代理设置**:为应对反爬策略,SmartSpider允许设置代理服务器,实现IP轮换。 3. **HTML解析模块** - **...
9. **CookieStore**: 管理HTTP Cookie,支持存储和读取,以实现会话管理。 通过httpasyncclient-4.0-beta4.jar,开发者能够构建高性能的网络应用,实现高效的HTTP请求处理,同时还能享受到Apache HttpClient的其他...
client.DefaultRequestHeaders.Add("Cookie", "cookie_name=cookie_value"); client.MaxResponseContentBufferSize = 256000; ``` 对于更复杂的数据抓取任务,例如模拟表单提交或者处理JavaScript渲染的页面,可以...
6. **Cookie和Session管理**:某些网站需要登录才能访问,这时需要处理Cookie和Session以模拟用户会话。 7. **延迟和重试机制**:为避免对目标网站造成过大压力,应设置适当的延时和重试策略。 8. **异常处理**:...
- **`org.apache.commons.httpclient.cookie`**:处理Cookie的包,用于管理和跟踪HTTP会话中的Cookies。 - **`org.apache.commons.pool.impl`**:提供对象池的实现,用于复用对象,减少资源创建和销毁的成本,提高...
6. **Cookie管理**:为了保持用户会话,开发者可能会使用ASP.NET的Session或Cookie机制。在QQ登录成功后,可能将访问令牌存储在Cookie中,以便后续请求能识别已登录的QQ用户。 7. **数据持久化**:一旦获取到QQ用户...
4. **Cookie管理**:处理登录验证或会话保持。 5. **延迟和重试策略**:防止对目标网站造成过大压力,同时确保数据获取的成功率。 6. **动态内容抓取**:支持JavaScript渲染,如使用Selenium WebDriver或Blazor框架...
`commons-httpclient-3.1.jar`是Apache Commons HttpClient库,用于处理HTTP请求和响应,提供了高级的HTTP客户端功能,包括重试、代理设置、Cookie管理等。 `junit-dep-4.10.jar`是JUnit测试框架的旧版本,用于编写...
5. **网页状态管理**:登录、cookie和session管理对于访问需要登录权限或者有会话状态的网站至关重要。Java的HttpURLConnection或HttpClient库都能处理这些问题。 6. **爬虫框架**:除了基础的编程实现,还有一些...
4. **定时器与事件驱动**:为了在商品开售后立即发起抢购,软件可能会使用定时器来监控抢购时间点,一旦到达设定时间,立即触发抢购逻辑。C#中的System.Timers.Timer或System.Threading.Timer可以实现这一功能。 5....
8. **反反爬策略**:很多网站会有反爬虫机制,如设置User-Agent、Cookie、验证码等。Java Web数据采集系统需要适当地模拟浏览器行为,遵守网站的Robots协议,防止被识别为机器人。 9. **数据清洗与预处理**:采集到...