⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpstate.java

📁 Light in the box 抓取程序。 使用HttpClient
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpState.java,v 1.38 2004/12/20 11:50:54 olegk Exp $ * $Revision: 561099 $ * $Date: 2007-07-30 21:41:17 +0200 (Mon, 30 Jul 2007) $ * * ==================================================================== * *  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;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.List;import java.util.Iterator;import org.apache.commons.httpclient.cookie.CookieSpec;import org.apache.commons.httpclient.cookie.CookiePolicy;import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * <p> * A container for HTTP attributes that may persist from request * to request, such as {@link Cookie cookies} and authentication * {@link Credentials credentials}. * </p> *  * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> * @author Rodney Waldhoff * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> * @author Sean C. Sullivan * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a> * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a> *  * @version $Revision: 561099 $ $Date: 2007-07-30 21:41:17 +0200 (Mon, 30 Jul 2007) $ *  */public class HttpState {    // ----------------------------------------------------- Instance Variables    /**     * Map of {@link Credentials credentials} by realm that this      * HTTP state contains.     */    protected HashMap credMap = new HashMap();    /**     * Map of {@link Credentials proxy credentials} by realm that this     * HTTP state contains     */    protected HashMap proxyCred = new HashMap();    /**     * Array of {@link Cookie cookies} that this HTTP state contains.     */    protected ArrayList cookies = new ArrayList();    private boolean preemptive = false;    private int cookiePolicy = -1;    // -------------------------------------------------------- Class Variables    /**     * The boolean system property name to turn on preemptive authentication.     * @deprecated This field and feature will be removed following HttpClient 3.0.     */    public static final String PREEMPTIVE_PROPERTY = "httpclient.authentication.preemptive";    /**     * The default value for {@link #PREEMPTIVE_PROPERTY}.     * @deprecated This field and feature will be removed following HttpClient 3.0.     */    public static final String PREEMPTIVE_DEFAULT = "false";        /** Log object for this class. */    private static final Log LOG = LogFactory.getLog(HttpState.class);    /**     * Default constructor.     */    public HttpState() {        super();    }    // ------------------------------------------------------------- Properties    /**     * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies.     * If the given cookie has already expired it will not be added, but existing      * values will still be removed.     *      * @param cookie the {@link Cookie cookie} to be added     *      * @see #addCookies(Cookie[])     *      */    public synchronized void addCookie(Cookie cookie) {        LOG.trace("enter HttpState.addCookie(Cookie)");        if (cookie != null) {            // first remove any old cookie that is equivalent            for (Iterator it = cookies.iterator(); it.hasNext();) {                Cookie tmp = (Cookie) it.next();                if (cookie.equals(tmp)) {                    it.remove();                    break;                }            }            if (!cookie.isExpired()) {                cookies.add(cookie);            }        }    }    /**     * Adds an array of {@link Cookie HTTP cookies}. Cookies are added individually and      * in the given array order. If any of the given cookies has already expired it will      * not be added, but existing values will still be removed.     *      * @param cookies the {@link Cookie cookies} to be added     *      * @see #addCookie(Cookie)     *      *      */    public synchronized void addCookies(Cookie[] cookies) {        LOG.trace("enter HttpState.addCookies(Cookie[])");        if (cookies != null) {            for (int i = 0; i < cookies.length; i++) {                this.addCookie(cookies[i]);            }        }    }    /**     * Returns an array of {@link Cookie cookies} that this HTTP     * state currently contains.     *      * @return an array of {@link Cookie cookies}.     *      * @see #getCookies(String, int, String, boolean)     *      */    public synchronized Cookie[] getCookies() {        LOG.trace("enter HttpState.getCookies()");        return (Cookie[]) (cookies.toArray(new Cookie[cookies.size()]));    }    /**     * Returns an array of {@link Cookie cookies} in this HTTP      * state that match the given request parameters.     *      * @param domain the request domain     * @param port the request port     * @param path the request path     * @param secure <code>true</code> when using HTTPS     *      * @return an array of {@link Cookie cookies}.     *      * @see #getCookies()     *      * @deprecated use CookieSpec#match(String, int, String, boolean, Cookie)     */    public synchronized Cookie[] getCookies(        String domain,         int port,         String path,         boolean secure    ) {        LOG.trace("enter HttpState.getCookies(String, int, String, boolean)");        CookieSpec matcher = CookiePolicy.getDefaultSpec();        ArrayList list = new ArrayList(cookies.size());        for (int i = 0, m = cookies.size(); i < m; i++) {            Cookie cookie = (Cookie) (cookies.get(i));            if (matcher.match(domain, port, path, secure, cookie)) {                list.add(cookie);            }        }        return (Cookie[]) (list.toArray(new Cookie[list.size()]));    }    /**     * Removes all of {@link Cookie cookies} in this HTTP state     * that have expired according to the current system time.     *      * @see #purgeExpiredCookies(java.util.Date)     *      */    public synchronized boolean purgeExpiredCookies() {        LOG.trace("enter HttpState.purgeExpiredCookies()");        return purgeExpiredCookies(new Date());    }    /**     * Removes all of {@link Cookie cookies} in this HTTP state     * that have expired by the specified {@link java.util.Date date}.      *      * @param date The {@link java.util.Date date} to compare against.     *      * @return true if any cookies were purged.     *      * @see Cookie#isExpired(java.util.Date)     *      * @see #purgeExpiredCookies()     */    public synchronized boolean purgeExpiredCookies(Date date) {        LOG.trace("enter HttpState.purgeExpiredCookies(Date)");        boolean removed = false;        Iterator it = cookies.iterator();        while (it.hasNext()) {            if (((Cookie) (it.next())).isExpired(date)) {                it.remove();                removed = true;            }        }        return removed;    }    /**     * Returns the current {@link CookiePolicy cookie policy} for this     * HTTP state.     *      * @return The {@link CookiePolicy cookie policy}.     *      * @deprecated Use      *  {@link org.apache.commons.httpclient.params.HttpMethodParams#getCookiePolicy()},     *  {@link HttpMethod#getParams()}.          */        public int getCookiePolicy() {        return this.cookiePolicy;    }        /**     * Defines whether preemptive authentication should be      * attempted.     *      * @param value <tt>true</tt> if preemptive authentication should be      * attempted, <tt>false</tt> otherwise.      *      * @deprecated Use      * {@link org.apache.commons.httpclient.params.HttpClientParams#setAuthenticationPreemptive(boolean)},      * {@link HttpClient#getParams()}.     */        public void setAuthenticationPreemptive(boolean value) {        this.preemptive = value;    }    /**     * Returns <tt>true</tt> if preemptive authentication should be      * attempted, <tt>false</tt> otherwise.     *      * @return boolean flag.     *      * @deprecated Use      * {@link org.apache.commons.httpclient.params.HttpClientParams#isAuthenticationPreemptive()},      * {@link HttpClient#getParams()}.     */        public boolean isAuthenticationPreemptive() {        return this.preemptive;    }        /**     * Sets the current {@link CookiePolicy cookie policy} for this HTTP     * state to one of the following supported policies:      * {@link CookiePolicy#COMPATIBILITY},      * {@link CookiePolicy#NETSCAPE_DRAFT} or     * {@link CookiePolicy#RFC2109}.     *      * @param policy new {@link CookiePolicy cookie policy}     *      * @deprecated      *  Use {@link org.apache.commons.httpclient.params.HttpMethodParams#setCookiePolicy(String)},     *  {@link HttpMethod#getParams()}.          */        public void setCookiePolicy(int policy) {        this.cookiePolicy = policy;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -