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

📄 httpmethodbase.java

📁 爬虫
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * @return <tt>true</tt> to use HTTP/1.1, <tt>false</tt> to use 1.0     *      * @deprecated Use {@link HttpMethodParams#getVersion()}     */    public boolean isHttp11() {        return this.params.getVersion().equals(HttpVersion.HTTP_1_1);    }    /**     * Sets the path of the HTTP method.     * It is responsibility of the caller to ensure that the path is     * properly encoded (URL safe).     *     * @param path the path of the HTTP method. The path is expected     *        to be URL-encoded     */    public void setPath(String path) {        this.path = path;    }    /**     * Adds the specified request header, NOT overwriting any previous value.     * Note that header-name matching is case insensitive.     *     * @param header the header to add to the request     */    public void addRequestHeader(Header header) {        LOG.trace("HttpMethodBase.addRequestHeader(Header)");        if (header == null) {            LOG.debug("null header value ignored");        } else {            getRequestHeaderGroup().addHeader(header);        }    }    /**     * Use this method internally to add footers.     *      * @param footer The footer to add.     */    public void addResponseFooter(Header footer) {        getResponseTrailerHeaderGroup().addHeader(footer);    }    /**     * Gets the path of this HTTP method.     * Calling this method <em>after</em> the request has been executed will      * return the <em>actual</em> path, following any redirects automatically     * handled by this HTTP method.     *     * @return the path to request or "/" if the path is blank.     */    public String getPath() {        return (path == null || path.equals("")) ? "/" : path;    }    /**     * Sets the query string of this HTTP method. The caller must ensure that the string      * is properly URL encoded. The query string should not start with the question      * mark character.     *     * @param queryString the query string     *      * @see EncodingUtil#formUrlEncode(NameValuePair[], String)     */    public void setQueryString(String queryString) {        this.queryString = queryString;    }    /**     * Sets the query string of this HTTP method.  The pairs are encoded as UTF-8 characters.       * To use a different charset the parameters can be encoded manually using EncodingUtil      * and set as a single String.     *     * @param params an array of {@link NameValuePair}s to add as query string     *        parameters. The name/value pairs will be automcatically      *        URL encoded     *      * @see EncodingUtil#formUrlEncode(NameValuePair[], String)     * @see #setQueryString(String)     */    public void setQueryString(NameValuePair[] params) {        LOG.trace("enter HttpMethodBase.setQueryString(NameValuePair[])");        queryString = EncodingUtil.formUrlEncode(params, "UTF-8");    }    /**     * Gets the query string of this HTTP method.     *     * @return The query string     */    public String getQueryString() {        return queryString;    }    /**     * Set the specified request header, 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 setRequestHeader(String headerName, String headerValue) {        Header header = new Header(headerName, headerValue);        setRequestHeader(header);    }    /**     * Sets the specified request header, overwriting any previous value.     * Note that header-name matching is case insensitive.     *      * @param header the header     */    public void setRequestHeader(Header header) {                Header[] headers = getRequestHeaderGroup().getHeaders(header.getName());                for (int i = 0; i < headers.length; i++) {            getRequestHeaderGroup().removeHeader(headers[i]);        }                getRequestHeaderGroup().addHeader(header);            }    /**     * Returns the specified request header. Note that header-name matching is     * case insensitive. <tt>null</tt> will be returned if either     * <i>headerName</i> is <tt>null</tt> or there is no matching header for     * <i>headerName</i>.     *      * @param headerName The name of the header to be returned.     *     * @return The specified request header.     *      * @since 3.0     */    public Header getRequestHeader(String headerName) {        if (headerName == null) {            return null;        } else {            return getRequestHeaderGroup().getCondensedHeader(headerName);        }    }    /**     * Returns an array of the requests headers that the HTTP method currently has     *     * @return an array of my request headers.     */    public Header[] getRequestHeaders() {        return getRequestHeaderGroup().getAllHeaders();    }    /**     * @see org.apache.commons.httpclient.HttpMethod#getRequestHeaders(java.lang.String)     */    public Header[] getRequestHeaders(String headerName) {        return getRequestHeaderGroup().getHeaders(headerName);    }    /**     * Gets the {@link HeaderGroup header group} storing the request headers.     *      * @return a HeaderGroup     *      * @since 2.0beta1     */    protected HeaderGroup getRequestHeaderGroup() {        return requestHeaders;    }    /**     * Gets the {@link HeaderGroup header group} storing the response trailer headers      * as per RFC 2616 section 3.6.1.     *      * @return a HeaderGroup     *      * @since 2.0beta1     */    protected HeaderGroup getResponseTrailerHeaderGroup() {        return responseTrailerHeaders;    }    /**     * Gets the {@link HeaderGroup header group} storing the response headers.     *      * @return a HeaderGroup     *      * @since 2.0beta1     */    protected HeaderGroup getResponseHeaderGroup() {        return responseHeaders;    }        /**     * @see org.apache.commons.httpclient.HttpMethod#getResponseHeaders(java.lang.String)     *      * @since 3.0     */    public Header[] getResponseHeaders(String headerName) {        return getResponseHeaderGroup().getHeaders(headerName);    }    /**     * Returns the response status code.     *     * @return the status code associated with the latest response.     */    public int getStatusCode() {        return statusLine.getStatusCode();    }    /**     * Provides access to the response status line.     *     * @return the status line object from the latest response.     * @since 2.0     */    public StatusLine getStatusLine() {        return statusLine;    }    /**     * Checks if response data is available.     * @return <tt>true</tt> if response data is available, <tt>false</tt> otherwise.     */    private boolean responseAvailable() {        return (responseBody != null) || (responseStream != null);    }    /**     * Returns an array of the response headers that the HTTP method currently has     * in the order in which they were read.     *     * @return an array of response headers.     */    public Header[] getResponseHeaders() {        return getResponseHeaderGroup().getAllHeaders();    }    /**     * Gets the response header associated with the given name. Header name     * matching is case insensitive. <tt>null</tt> will be returned if either     * <i>headerName</i> is <tt>null</tt> or there is no matching header for     * <i>headerName</i>.     *     * @param headerName the header name to match     *     * @return the matching header     */    public Header getResponseHeader(String headerName) {                if (headerName == null) {            return null;        } else {            return getResponseHeaderGroup().getCondensedHeader(headerName);        }            }    /**     * Return the length (in bytes) of the response body, as specified in a     * <tt>Content-Length</tt> header.     *     * <p>     * Return <tt>-1</tt> when the content-length is unknown.     * </p>     *     * @return content length, if <tt>Content-Length</tt> header is available.      *          <tt>0</tt> indicates that the request has no body.     *          If <tt>Content-Length</tt> header is not present, the method      *          returns  <tt>-1</tt>.     */    public long getResponseContentLength() {        Header[] headers = getResponseHeaderGroup().getHeaders("Content-Length");        if (headers.length == 0) {            return -1;        }        if (headers.length > 1) {            LOG.warn("Multiple content-length headers detected");        }        for (int i = headers.length - 1; i >= 0; i--) {            Header header = headers[i];            try {                return Long.parseLong(header.getValue());            } catch (NumberFormatException e) {                if (LOG.isWarnEnabled()) {                    LOG.warn("Invalid content-length value: " + e.getMessage());                }            }            // See if we can have better luck with another header, if present        }        return -1;    }    /**     * Returns the response body of the HTTP method, if any, as an array of bytes.     * If response body is not available or cannot be read, returns <tt>null</tt>     *      * 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 byte[] getResponseBody() throws IOException {        if (this.responseBody == null) {            InputStream instream = getResponseBodyAsStream();            if (instream != null) {                long contentLength = getResponseContentLength();                if (contentLength > Integer.MAX_VALUE) { //guard below cast from overflow                    throw new IOException("Content too large to be buffered: "+ contentLength +" bytes");                }                int limit = getParams().getIntParameter(HttpMethodParams.BUFFER_WARN_TRIGGER_LIMIT, 1024*1024);                if ((contentLength == -1) || (contentLength > limit)) {                    LOG.warn("Going to buffer response body of large or unknown size. "                            +"Using getResponseAsStream instead is recommended.");                }                LOG.debug("Buffering response body");                ByteArrayOutputStream outstream = new ByteArrayOutputStream(                        contentLength > 0 ? (int) contentLength : DEFAULT_INITIAL_BUFFER_SIZE);                byte[] buffer = new byte[4096];                int len;                while ((len = instream.read(buffer)) > 0) {                    outstream.write(buffer, 0, len);                }                outstream.close();                setResponseStream(null);                this.responseBody = outstream.toByteArray();            }        }        return this.responseBody;    }    /**     * Returns the response body of the HTTP method, if any, as an {@link InputStream}.      * If response body is not available, returns <tt>null</tt>     *      * @return The response body     *      * @throws IOException If an I/O (transport) problem occurs while obtaining the      * response body.     */    public InputStream getResponseBodyAsStream() throws IOException {        if (responseStream != null) {            return responseStream;        }        if (responseBody != null) {            InputStream byteResponseStream = new ByteArrayInputStream(responseBody);            LOG.debug("re-creating response stream from byte array");            return byteResponseStream;        }        return null;

⌨️ 快捷键说明

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