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

📄 httpmethodbase.java

📁 Light in the box 抓取程序。 使用HttpClient
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * @param state {@link HttpState state} information to associate with this     *        request. Must be non-null.     * @param conn the {@link HttpConnection connection} to used to execute     *        this HTTP method. Must be non-null.     *     * @return the integer status code if one was obtained, or <tt>-1</tt>     *     * @throws IOException if an I/O (transport) error occurs     * @throws HttpException  if a protocol exception occurs.     */    public int execute(HttpState state, HttpConnection conn)        throws HttpException, IOException {                        LOG.trace("enter HttpMethodBase.execute(HttpState, HttpConnection)");        // this is our connection now, assign it to a local variable so         // that it can be released later        this.responseConnection = conn;        checkExecuteConditions(state, conn);        this.statusLine = null;        this.connectionCloseForced = false;        conn.setLastResponseInputStream(null);        // determine the effective protocol version        if (this.effectiveVersion == null) {            this.effectiveVersion = this.params.getVersion();         }        writeRequest(state, conn);        this.requestSent = true;        readResponse(state, conn);        // the method has successfully executed        used = true;         return statusLine.getStatusCode();    }    /**     * Aborts the execution of this method.     *      * @since 3.0     */    public void abort() {        if (this.aborted) {            return;        }        this.aborted = true;        HttpConnection conn = this.responseConnection;         if (conn != null) {            conn.close();        }    }    /**     * Returns <tt>true</tt> if the HTTP method has been already {@link #execute executed},     * but not {@link #recycle recycled}.     *      * @return <tt>true</tt> if the method has been executed, <tt>false</tt> otherwise     */    public boolean hasBeenUsed() {        return used;    }    /**     * Recycles the HTTP method so that it can be used again.     * Note that all of the instance variables will be reset     * once this method has been called. This method will also     * release the connection being used by this HTTP method.     *      * @see #releaseConnection()     *      * @deprecated no longer supported and will be removed in the future     *             version of HttpClient     */    public void recycle() {        LOG.trace("enter HttpMethodBase.recycle()");        releaseConnection();        path = null;        followRedirects = false;        doAuthentication = true;        queryString = null;        getRequestHeaderGroup().clear();        getResponseHeaderGroup().clear();        getResponseTrailerHeaderGroup().clear();        statusLine = null;        effectiveVersion = null;        aborted = false;        used = false;        params = new HttpMethodParams();        responseBody = null;        recoverableExceptionCount = 0;        connectionCloseForced = false;        hostAuthState.invalidate();        proxyAuthState.invalidate();        cookiespec = null;        requestSent = false;    }    /**     * Releases the connection being used by this HTTP method. In particular the     * connection is used to read the response(if there is one) and will be held     * until the response has been read. If the connection can be reused by other      * HTTP methods it is NOT closed at this point.     *     * @since 2.0     */    public void releaseConnection() {        try {            if (this.responseStream != null) {                try {                    // FYI - this may indirectly invoke responseBodyConsumed.                    this.responseStream.close();                } catch (IOException ignore) {                }            }        } finally {            ensureConnectionRelease();        }    }    /**     * Remove the request header associated with the given name. Note that     * header-name matching is case insensitive.     *     * @param headerName the header name     */    public void removeRequestHeader(String headerName) {                Header[] headers = getRequestHeaderGroup().getHeaders(headerName);        for (int i = 0; i < headers.length; i++) {            getRequestHeaderGroup().removeHeader(headers[i]);        }            }        /**     * Removes the given request header.     *      * @param header the header     */    public void removeRequestHeader(final Header header) {        if (header == null) {            return;        }        getRequestHeaderGroup().removeHeader(header);    }    // ---------------------------------------------------------------- Queries    /**     * Returns <tt>true</tt> the method is ready to execute, <tt>false</tt> otherwise.     *      * @return This implementation always returns <tt>true</tt>.     */    public boolean validate() {        return true;    }    /**      * Returns the actual cookie policy     *      * @param state HTTP state. TODO: to be removed in the future     *      * @return cookie spec     */    private CookieSpec getCookieSpec(final HttpState state) {        if (this.cookiespec == null) {            int i = state.getCookiePolicy();            if (i == -1) {                this.cookiespec = CookiePolicy.getCookieSpec(this.params.getCookiePolicy());            } else {                this.cookiespec = CookiePolicy.getSpecByPolicy(i);            }            this.cookiespec.setValidDateFormats(                    (Collection)this.params.getParameter(HttpMethodParams.DATE_PATTERNS));        }        return this.cookiespec;    }    /**     * Generates <tt>Cookie</tt> request headers for those {@link Cookie cookie}s     * that match the given host, port and path.     *     * @param state the {@link HttpState state} information associated with this method     * @param conn the {@link HttpConnection connection} used to execute     *        this HTTP method     *     * @throws IOException if an I/O (transport) error occurs. Some transport exceptions     *                     can be recovered from.     * @throws HttpException  if a protocol exception occurs. Usually protocol exceptions      *                    cannot be recovered from.     */    protected void addCookieRequestHeader(HttpState state, HttpConnection conn)        throws IOException, HttpException {        LOG.trace("enter HttpMethodBase.addCookieRequestHeader(HttpState, "                  + "HttpConnection)");        Header[] cookieheaders = getRequestHeaderGroup().getHeaders("Cookie");        for (int i = 0; i < cookieheaders.length; i++) {            Header cookieheader = cookieheaders[i];            if (cookieheader.isAutogenerated()) {                getRequestHeaderGroup().removeHeader(cookieheader);            }        }        CookieSpec matcher = getCookieSpec(state);        String host = this.params.getVirtualHost();        if (host == null) {            host = conn.getHost();        }        Cookie[] cookies = matcher.match(host, conn.getPort(),            getPath(), conn.isSecure(), state.getCookies());        if ((cookies != null) && (cookies.length > 0)) {            if (getParams().isParameterTrue(HttpMethodParams.SINGLE_COOKIE_HEADER)) {                // In strict mode put all cookies on the same header                String s = matcher.formatCookies(cookies);                getRequestHeaderGroup().addHeader(new Header("Cookie", s, true));            } else {                // In non-strict mode put each cookie on a separate header                for (int i = 0; i < cookies.length; i++) {                    String s = matcher.formatCookie(cookies[i]);                    getRequestHeaderGroup().addHeader(new Header("Cookie", s, true));                }            }            if (matcher instanceof CookieVersionSupport) {                CookieVersionSupport versupport = (CookieVersionSupport) matcher;                int ver = versupport.getVersion();                boolean needVersionHeader = false;                for (int i = 0; i < cookies.length; i++) {                    if (ver != cookies[i].getVersion()) {                        needVersionHeader = true;                    }                }                if (needVersionHeader) {                    // Advertise cookie version support                    getRequestHeaderGroup().addHeader(versupport.getVersionHeader());                }            }        }    }    /**     * Generates <tt>Host</tt> request header, as long as no <tt>Host</tt> request     * header already exists.     *     * @param state the {@link HttpState state} information associated with this method     * @param conn the {@link HttpConnection connection} used to execute     *        this HTTP method     *     * @throws IOException if an I/O (transport) error occurs. Some transport exceptions     *                     can be recovered from.     * @throws HttpException  if a protocol exception occurs. Usually protocol exceptions      *                    cannot be recovered from.     */    protected void addHostRequestHeader(HttpState state, HttpConnection conn)    throws IOException, HttpException {        LOG.trace("enter HttpMethodBase.addHostRequestHeader(HttpState, "                  + "HttpConnection)");        // Per 19.6.1.1 of RFC 2616, it is legal for HTTP/1.0 based        // applications to send the Host request-header.        // TODO: Add the ability to disable the sending of this header for        //       HTTP/1.0 requests.        String host = this.params.getVirtualHost();        if (host != null) {            LOG.debug("Using virtual host name: " + host);        } else {            host = conn.getHost();        }        int port = conn.getPort();        // Note: RFC 2616 uses the term "internet host name" for what goes on the        // host line.  It would seem to imply that host should be blank if the        // host is a number instead of an name.  Based on the behavior of web        // browsers, and the fact that RFC 2616 never defines the phrase "internet        // host name", and the bad behavior of HttpClient that follows if we        // send blank, I interpret this as a small misstatement in the RFC, where        // they meant to say "internet host".  So IP numbers get sent as host        // entries too. -- Eric Johnson 12/13/2002        if (LOG.isDebugEnabled()) {            LOG.debug("Adding Host request header");        }        //appends the port only if not using the default port for the protocol        if (conn.getProtocol().getDefaultPort() != port) {            host += (":" + port);        }        setRequestHeader("Host", host);    }    /**     * Generates <tt>Proxy-Connection: Keep-Alive</tt> request header when      * communicating via a proxy server.     *     * @param state the {@link HttpState state} information associated with this method     * @param conn the {@link HttpConnection connection} used to execute     *        this HTTP method     *     * @throws IOException if an I/O (transport) error occurs. Some transport exceptions     *                     can be recovered from.     * @throws HttpException  if a protocol exception occurs. Usually protocol exceptions      *                    cannot be recovered from.     */    protected void addProxyConnectionHeader(HttpState state,                                            HttpConnection conn)    throws IOException, HttpException {        LOG.trace("enter HttpMethodBase.addProxyConnectionHeader("                  + "HttpState, HttpConnection)");        if (!conn.isTransparent()) {            if (getRequestHeader("Proxy-Connection") == null) {                addRequestHeader("Proxy-Connection", "Keep-Alive");            }        }    }    /**     * Generates all the required request {@link Header header}s      * to be submitted via the given {@link HttpConnection connection}.     *     * <p>     * This implementation adds <tt>User-Agent</tt>, <tt>Host</tt>,     * <tt>Cookie</tt>, <tt>Authorization</tt>, <tt>Proxy-Authorization</tt>     * and <tt>Proxy-Connection</tt> headers, when appropriate.     * </p>     *     * <p>     * Subclasses may want to override this method to to add additional     * headers, and may choose to invoke this implementation (via     * <tt>super</tt>) to add the "standard" headers.     * </p>     *     * @param state the {@link HttpState state} information associated with this method     * @param conn the {@link HttpConnection connection} used to execute     *        this HTTP method     *     * @throws IOException if an I/O (transport) error occurs. Some transport exceptions     *                     can be recovered from.     * @throws HttpException  if a protocol exception occurs. Usually protocol exceptions      *                    cannot be recovered from.     *     * @see #writeRequestHeaders     */    protected void addRequestHeaders(HttpState state, HttpConnection conn)    throws IOException, HttpException {        LOG.trace("enter HttpMethodBase.addRequestHeaders(HttpState, "            + "HttpConnection)");        addUserAgentRequestHeader(state, conn);

⌨️ 快捷键说明

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