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

📄 httpmethodbase.java

📁 爬虫
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    public void releaseConnection() {        if (responseStream != null) {            try {                // FYI - this may indirectly invoke responseBodyConsumed.                responseStream.close();            } catch (IOException e) {                // the connection may not have been released, let's make sure                ensureConnectionRelease();            }        } else {            // Make sure the connection has been released. If the response             // stream has not been set, this is the only way to release the             // connection.             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);// BEGIN IA CHANGES//      PRIOR IMPL & COMPARISON HARNESS LEFT COMMENTED OUT FOR TEMPORARY REFERENCE//        Cookie[] arrayListAnswer;        Cookie[] cookies;        synchronized(state) {//            arrayListAnswer = matcher.match(conn.getHost(), conn.getPort(),//                getPath(), conn.isSecure(), state.getCookies());            cookies = matcher.match(conn.getHost(), conn.getPort(),                    getPath(), conn.isSecure(), state.getCookiesMap());//            if(! (new HashSet(Arrays.asList(arrayListAnswer)).equals(new HashSet(Arrays.asList(cookies))))) {//                System.out.println("discrepancy");//                arrayListAnswer = matcher.match(conn.getHost(), conn.getPort(),//                        getPath(), conn.isSecure(), state.getCookies());//                cookies = matcher.match(conn.getHost(), conn.getPort(),//                            getPath(), conn.isSecure(), state.getCookiesMap());//            }// END IA CHANGES        }        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));                }            }        }    }    /**     * 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()) {            setRequestHeader("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);        addHostRequestHeader(state, conn);        addCookieRequestHeader(state, conn);        addProxyConnectionHeader(state, conn);    }    /**     * Generates default <tt>User-Agent</tt> request header, as long as no     * <tt>User-Agent</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 addUserAgentRequestHeader(HttpState state,                                             HttpConnection conn)    throws IOException, HttpException {        LOG.trace("enter HttpMethodBase.addUserAgentRequestHeaders(HttpState, "            + "HttpConnection)");        if (getRequestHeader("User-Agent") == null) {            String agent = (String)getParams().getParameter(HttpMethodParams.USER_AGENT);            if (agent == null) {                agent = "Jakarta Commons-HttpClient";            }            setRequestHeader("User-Agent", agent);        }    }    /**     * Throws an {@link IllegalStateException} if the HTTP method has been already     * {@link #execute executed}, but not {@link #recycle recycled}.     *     * @throws IllegalStateException if the method has been used and not     *      recycled     */    protected void checkNotUsed() throws IllegalStateException {        if (used) {            throw new IllegalStateException("Already used.");        }    }    /**     * Throws an {@link IllegalStateException} if the HTTP method has not been     * {@link #execute executed} since last {@link #recycle recycle}.     *     *     * @throws IllegalStateException if not used     */    protected void checkUsed()  throws IllegalStateException {        if (!used) {            throw new IllegalStateException("Not Used.");        }    }    // ------------------------------------------------- Static Utility Methods    /**     * Generates HTTP request line according to the specified attributes.     *     * @param connection the {@link HttpConnection connection} used to execute     *        this HTTP method     * @param name the method name generate a request for     * @param requestPath the path string for the request     * @param query the query string for the request     * @param version the protocol version to use (e.g. HTTP/1.0)     *     * @return HTTP request line     */    protected static String generateRequestLine(HttpConnection connection,        String name, String requestPath, String query, String version) {        LOG.trace("enter HttpMethodBase.generateRequestLine(HttpConnection, "            + "String, String, String, String)");        StringBuffer buf = new StringBuffer();        // Append method name        buf.append(name);        buf.append(" ");        // Absolute or relative URL?        if (!connection.isTransparent()) {            Protocol protocol = connection.getProtocol();            buf.append(protocol.getScheme().toLowerCase());            buf.append("://");            buf.append(connection.getHost());            if ((connection.getPort() != -1)                 && (connection.getPort() != protocol.getDefaultPort())            ) {                buf.append(":");                buf.append(connection.getPort());            }        }        // Append path, if any        if (requestPath == null) {            buf.append("/");        } else {            if (!connection.isTransparent() && !requestPath.startsWith("/")) {                buf.append("/");            }            buf.append(requestPath);        }        // Append query, if any        if (query != null) {            if (query.indexOf("?") != 0) {                buf.append("?");            }            buf.append(query);        }        // Append protocol        buf.append(" ");

⌨️ 快捷键说明

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