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

📄 httpconnection.java

📁 爬虫
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * @param data the string to be written     * @param charset the charset to use for writing the data     * @throws IllegalStateException if the connection is not open     * @throws IOException if an I/O problem occurs     *      * @since 3.0     */    public void print(String data, String charset)        throws IOException, IllegalStateException {        LOG.trace("enter HttpConnection.print(String)");        write(EncodingUtil.getBytes(data, charset));    }        /**     * @deprecated Use {@link #printLine(String, String)}     *      * Writes the specified String (as bytes), followed by     * <tt>"\r\n".getBytes()</tt> to the output stream.     *     * @param data the data to be written     * @throws IllegalStateException if the connection is not open     * @throws IOException if an I/O problem occurs     */    public void printLine(String data)        throws IOException, IllegalStateException {        LOG.trace("enter HttpConnection.printLine(String)");        writeLine(EncodingUtil.getBytes(data, "ISO-8859-1"));    }    /**     * Writes the specified String (as bytes), followed by     * <tt>"\r\n".getBytes()</tt> to the output stream.     *     * @param data the data to be written     * @param charset the charset to use for writing the data     * @throws IllegalStateException if the connection is not open     * @throws IOException if an I/O problem occurs     *      * @since 3.0     */    public void printLine(String data, String charset)        throws IOException, IllegalStateException {        LOG.trace("enter HttpConnection.printLine(String)");        writeLine(EncodingUtil.getBytes(data, charset));    }            /**     * Writes <tt>"\r\n".getBytes()</tt> to the output stream.     *     * @throws IllegalStateException if the connection is not open     * @throws IOException if an I/O problem occurs     */    public void printLine()        throws IOException, IllegalStateException {        LOG.trace("enter HttpConnection.printLine()");        writeLine();    }    /**     * Reads up to <tt>"\n"</tt> from the (unchunked) input stream.     * If the stream ends before the line terminator is found,     * the last part of the string will still be returned.     *     * @throws IllegalStateException if the connection is not open     * @throws IOException if an I/O problem occurs     * @return a line from the response     *      * @deprecated use #readLine(String)     */    public String readLine() throws IOException, IllegalStateException {        LOG.trace("enter HttpConnection.readLine()");        assertOpen();        return HttpParser.readLine(inputStream);    }    /**     * Reads up to <tt>"\n"</tt> from the (unchunked) input stream.     * If the stream ends before the line terminator is found,     * the last part of the string will still be returned.     *      * @param charset the charset to use for reading the data     *     * @throws IllegalStateException if the connection is not open     * @throws IOException if an I/O problem occurs     * @return a line from the response     *      * @since 3.0     */    public String readLine(final String charset) throws IOException, IllegalStateException {        LOG.trace("enter HttpConnection.readLine()");        assertOpen();        return HttpParser.readLine(inputStream, charset);    }    /**     * Attempts to shutdown the {@link Socket}'s output, via Socket.shutdownOutput()     * when running on JVM 1.3 or higher.     *      * @deprecated unused     */    public void shutdownOutput() {        LOG.trace("enter HttpConnection.shutdownOutput()");        try {            // Socket.shutdownOutput is a JDK 1.3            // method. We'll use reflection in case            // we're running in an older VM            Class[] paramsClasses = new Class[0];            Method shutdownOutput =                socket.getClass().getMethod("shutdownOutput", paramsClasses);            Object[] params = new Object[0];            shutdownOutput.invoke(socket, params);        } catch (Exception ex) {            LOG.debug("Unexpected Exception caught", ex);            // Ignore, and hope everything goes right        }        // close output stream?    }    /**     * Closes the socket and streams.     */    public void close() {        LOG.trace("enter HttpConnection.close()");        closeSocketAndStreams();    }    /**     * Returns the httpConnectionManager.     * @return HttpConnectionManager     */    public HttpConnectionManager getHttpConnectionManager() {        return httpConnectionManager;    }    /**     * Sets the httpConnectionManager.     * @param httpConnectionManager The httpConnectionManager to set     */    public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) {        this.httpConnectionManager = httpConnectionManager;    }    /**     * Releases the connection. If the connection is locked or does not have a connection     * manager associated with it, this method has no effect. Note that it is completely safe      * to call this method multiple times.     */    public void releaseConnection() {        LOG.trace("enter HttpConnection.releaseConnection()");        if (locked) {            LOG.debug("Connection is locked.  Call to releaseConnection() ignored.");        } else if (httpConnectionManager != null) {            LOG.debug("Releasing connection back to connection manager.");            httpConnectionManager.releaseConnection(this);        } else {            LOG.warn("HttpConnectionManager is null.  Connection cannot be released.");        }    }    /**     * Tests if the connection is locked. Locked connections cannot be released.      * An attempt to release a locked connection will have no effect.     *      * @return <tt>true</tt> if the connection is locked, <tt>false</tt> otherwise.     *      * @since 3.0     */    protected boolean isLocked() {        return locked;    }    /**     * Locks or unlocks the connection. Locked connections cannot be released.      * An attempt to release a locked connection will have no effect.     *      * @param locked <tt>true</tt> to lock the connection, <tt>false</tt> to unlock     *  the connection.     *      * @since 3.0     */    protected void setLocked(boolean locked) {        this.locked = locked;    }    // ------------------------------------------------------ Protected Methods    /**     * Closes everything out.     */    protected void closeSocketAndStreams() {        LOG.trace("enter HttpConnection.closeSockedAndStreams()");        // no longer care about previous responses...        lastResponseInputStream = null;        if (null != outputStream) {            OutputStream temp = outputStream;            outputStream = null;            try {                temp.close();            } catch (Exception ex) {                LOG.debug("Exception caught when closing output", ex);                // ignored            }        }        if (null != inputStream) {            InputStream temp = inputStream;            inputStream = null;            try {                temp.close();            } catch (Exception ex) {                LOG.debug("Exception caught when closing input", ex);                // ignored            }        }        if (null != socket) {            Socket temp = socket;            socket = null;            try {                temp.close();            } catch (Exception ex) {                LOG.debug("Exception caught when closing socket", ex);                // ignored            }        }        isOpen = false;        tunnelEstablished = false;        usingSecureSocket = false;    }    /**     * Throws an {@link IllegalStateException} if the connection is already open.     *     * @throws IllegalStateException if connected     */    protected void assertNotOpen() throws IllegalStateException {        if (isOpen) {            throw new IllegalStateException("Connection is open");        }    }    /**     * Throws an {@link IllegalStateException} if the connection is not open.     *     * @throws IllegalStateException if not connected     */    protected void assertOpen() throws IllegalStateException {        if (!isOpen) {            throw new IllegalStateException("Connection is not open");        }    }    /**     * Gets the socket's sendBufferSize.     *      * @return the size of the buffer for the socket OutputStream, -1 if the value     * has not been set and the socket has not been opened     *      * @throws SocketException if an error occurs while getting the socket value     *      * @see Socket#getSendBufferSize()     */    public int getSendBufferSize() throws SocketException {        if (socket == null) {            return -1;        } else {            return socket.getSendBufferSize();        }    }    /**     * Sets the socket's sendBufferSize.     *      * @param sendBufferSize the size to set for the socket OutputStream     *      * @throws SocketException if an error occurs while setting the socket value     *      * @see Socket#setSendBufferSize(int)     *      * @deprecated Use {@link HttpConnectionParams#setSendBufferSize(int)},     * {@link HttpConnection#getParams()}.     */    public void setSendBufferSize(int sendBufferSize) throws SocketException {        this.params.setSendBufferSize(sendBufferSize);    }    // ------------------------------------------------------- Static Variable    /** <tt>"\r\n"</tt>, as bytes. */    private static final byte[] CRLF = new byte[] {(byte) 13, (byte) 10};    /** Log object for this class. */    private static final Log LOG = LogFactory.getLog(HttpConnection.class);        // ----------------------------------------------------- Instance Variables        /** My host. */    private String hostName = null;        /** My port. */    private int portNumber = -1;        /** My proxy host. */    private String proxyHostName = null;        /** My proxy port. */    private int proxyPortNumber = -1;        /** My client Socket. */    private Socket socket = null;        /** My InputStream. */    private InputStream inputStream = null;    /** My OutputStream. */    private OutputStream outputStream = null;        /** An {@link InputStream} for the response to an individual request. */    private InputStream lastResponseInputStream = null;        /** Whether or not the connection is connected. */    protected boolean isOpen = false;        /** the protocol being used */    private Protocol protocolInUse;        /** Collection of HTTP parameters associated with this HTTP connection*/    private HttpConnectionParams params = new HttpConnectionParams();        /** flag to indicate if this connection can be released, if locked the connection cannot be      * released */    private boolean locked = false;        /** Whether or not the socket is a secure one. */    private boolean usingSecureSocket = false;        /** Whether the connection is open via a secure tunnel or not */    private boolean tunnelEstablished = false;        /** the connection manager that created this connection or null */    private HttpConnectionManager httpConnectionManager;        /** The local interface on which the connection is created, or null for the default */    private InetAddress localAddress;}

⌨️ 快捷键说明

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