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

📄 httpmethodbase.java

📁 爬虫
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }    /**     * Returns the response body of the HTTP method, if any, as a {@link String}.      * If response body is not available or cannot be read, returns <tt>null</tt>     * The string conversion on the data is done using the character encoding specified     * in <tt>Content-Type</tt> header.     *      * Note: This will cause the entire response body to be buffered in memory. A     * malicious server may easily exhaust all the VM memory. It is strongly     * recommended, to use getResponseAsStream if the content length of the response     * is unknown or resonably large.     *      * @return The response body.     *      * @throws IOException If an I/O (transport) problem occurs while obtaining the      * response body.     */    public String getResponseBodyAsString() throws IOException {        byte[] rawdata = null;        if (responseAvailable()) {            rawdata = getResponseBody();        }        if (rawdata != null) {            return EncodingUtil.getString(rawdata, getResponseCharSet());        } else {            return null;        }    }    /**     * Returns an array of the response footers that the HTTP method currently has     * in the order in which they were read.     *     * @return an array of footers     */    public Header[] getResponseFooters() {        return getResponseTrailerHeaderGroup().getAllHeaders();    }    /**     * Gets the response footer associated with the given name.     * Footer name matching is case insensitive.     * <tt>null</tt> will be returned if either <i>footerName</i> is     * <tt>null</tt> or there is no matching footer for <i>footerName</i>     * or there are no footers available.  If there are multiple footers     * with the same name, there values will be combined with the ',' separator     * as specified by RFC2616.     *      * @param footerName the footer name to match     * @return the matching footer     */    public Header getResponseFooter(String footerName) {        if (footerName == null) {            return null;        } else {            return getResponseTrailerHeaderGroup().getCondensedHeader(footerName);        }    }    /**     * Sets the response stream.     * @param responseStream The new response stream.     */    protected void setResponseStream(InputStream responseStream) {        this.responseStream = responseStream;    }    /**     * Returns a stream from which the body of the current response may be read.     * If the method has not yet been executed, if <code>responseBodyConsumed</code>     * has been called, or if the stream returned by a previous call has been closed,     * <code>null</code> will be returned.     *     * @return the current response stream     */    protected InputStream getResponseStream() {        return responseStream;    }        /**     * Returns the status text (or "reason phrase") associated with the latest     * response.     *      * @return The status text.     */    public String getStatusText() {        return statusLine.getReasonPhrase();    }    /**     * Defines how strictly HttpClient follows the HTTP protocol specification       * (RFC 2616 and other relevant RFCs). In the strict mode HttpClient precisely     * implements the requirements of the specification, whereas in non-strict mode      * it attempts to mimic the exact behaviour of commonly used HTTP agents,      * which many HTTP servers expect.     *      * @param strictMode <tt>true</tt> for strict mode, <tt>false</tt> otherwise     *      * @deprecated Use {@link org.apache.commons.httpclient.params.HttpParams#setParameter(String, Object)}     * to exercise a more granular control over HTTP protocol strictness.     */    public void setStrictMode(boolean strictMode) {        if (strictMode) {            this.params.makeStrict();        } else {            this.params.makeLenient();        }    }    /**     * @deprecated Use {@link org.apache.commons.httpclient.params.HttpParams#setParameter(String, Object)}     * to exercise a more granular control over HTTP protocol strictness.     *     * @return <tt>false</tt>     */    public boolean isStrictMode() {        return false;    }    /**     * Adds the specified request header, NOT overwriting any previous value.     * Note that header-name matching is case insensitive.     *     * @param headerName the header's name     * @param headerValue the header's value     */    public void addRequestHeader(String headerName, String headerValue) {        addRequestHeader(new Header(headerName, headerValue));    }    /**     * Tests if the connection should be force-closed when no longer needed.     *      * @return <code>true</code> if the connection must be closed     */    protected boolean isConnectionCloseForced() {        return this.connectionCloseForced;    }    /**     * Sets whether or not the connection should be force-closed when no longer      * needed. This value should only be set to <code>true</code> in abnormal      * circumstances, such as HTTP protocol violations.      *      * @param b <code>true</code> if the connection must be closed, <code>false</code>     * otherwise.     */    protected void setConnectionCloseForced(boolean b) {        if (LOG.isDebugEnabled()) {            LOG.debug("Force-close connection: " + b);        }        this.connectionCloseForced = b;    }    /**     * Tests if the connection should be closed after the method has been executed.     * The connection will be left open when using HTTP/1.1 or if <tt>Connection:      * keep-alive</tt> header was sent.     *      * @param conn the connection in question     *      * @return boolean true if we should close the connection.     */    protected boolean shouldCloseConnection(HttpConnection conn) {        // Connection must be closed due to an abnormal circumstance         if (isConnectionCloseForced()) {            LOG.debug("Should force-close connection.");            return true;        }        Header connectionHeader = null;        // In case being connected via a proxy server        if (!conn.isTransparent()) {            // Check for 'proxy-connection' directive            connectionHeader = responseHeaders.getFirstHeader("proxy-connection");        }        // In all cases Check for 'connection' directive        // some non-complaint proxy servers send it instread of        // expected 'proxy-connection' directive        if (connectionHeader == null) {            connectionHeader = responseHeaders.getFirstHeader("connection");        }        // In case the response does not contain any explict connection        // directives, check whether the request does        if (connectionHeader == null) {            connectionHeader = requestHeaders.getFirstHeader("connection");        }        if (connectionHeader != null) {            if (connectionHeader.getValue().equalsIgnoreCase("close")) {                if (LOG.isDebugEnabled()) {                    LOG.debug("Should close connection in response to directive: "                         + connectionHeader.getValue());                }                return true;            } else if (connectionHeader.getValue().equalsIgnoreCase("keep-alive")) {                if (LOG.isDebugEnabled()) {                    LOG.debug("Should NOT close connection in response to directive: "                         + connectionHeader.getValue());                }                return false;            } else {                if (LOG.isDebugEnabled()) {                    LOG.debug("Unknown directive: " + connectionHeader.toExternalForm());                }            }        }        LOG.debug("Resorting to protocol version default close connection policy");        // missing or invalid connection header, do the default        if (this.effectiveVersion.greaterEquals(HttpVersion.HTTP_1_1)) {            if (LOG.isDebugEnabled()) {                LOG.debug("Should NOT close connection, using " + this.effectiveVersion.toString());            }        } else {            if (LOG.isDebugEnabled()) {                LOG.debug("Should close connection, using " + this.effectiveVersion.toString());            }        }        return this.effectiveVersion.lessEquals(HttpVersion.HTTP_1_0);    }        /**     * Tests if the this method is ready to be executed.     *      * @param state the {@link HttpState state} information associated with this method     * @param conn the {@link HttpConnection connection} to be used     * @throws HttpException If the method is in invalid state.     */    private void checkExecuteConditions(HttpState state, HttpConnection conn)    throws HttpException {        if (state == null) {            throw new IllegalArgumentException("HttpState parameter may not be null");        }        if (conn == null) {            throw new IllegalArgumentException("HttpConnection parameter may not be null");        }        if (this.aborted) {            throw new IllegalStateException("Method has been aborted");        }        if (!validate()) {            throw new ProtocolException("HttpMethodBase object not valid");        }    }    /**     * Executes this method using the specified <code>HttpConnection</code> and     * <code>HttpState</code>.      *     * @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     */

⌨️ 快捷键说明

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