Save This Page
Home » commons-httpclient-3.1-src » org.apache.commons » httpclient » [javadoc | source]
org.apache.commons.httpclient
public class: HttpConnection [javadoc | source]
java.lang.Object
   org.apache.commons.httpclient.HttpConnection

Direct Known Subclasses:
    HttpConnectionWithReference, SimpleHttpConnection, HttpConnectionAdapter, TimeoutHttpConnection

An abstraction of an HTTP InputStream and OutputStream pair, together with the relevant attributes.

The following options are set on the socket before getting the input/output streams in the #open() method:
Socket Method Sockets Option Configuration
java.net.Socket#setTcpNoDelay(boolean) SO_NODELAY HttpConnectionParams#setTcpNoDelay(boolean)
java.net.Socket#setSoTimeout(int) SO_TIMEOUT HttpConnectionParams#setSoTimeout(int)
java.net.Socket#setSendBufferSize(int) SO_SNDBUF HttpConnectionParams#setSendBufferSize(int)
java.net.Socket#setReceiveBufferSize(int) SO_RCVBUF HttpConnectionParams#setReceiveBufferSize(int)

Field Summary
protected  boolean isOpen    Whether or not the connection is connected. 
Constructor:
 public HttpConnection(HostConfiguration hostConfiguration) 
    Creates a new HTTP connection for the given host configuration.
    Parameters:
    hostConfiguration - the host/proxy/protocol to use
 public HttpConnection(String host,
    int port) 
    Creates a new HTTP connection for the given host and port.
    Parameters:
    host - the host to connect to
    port - the port to connect to
 public HttpConnection(String host,
    int port,
    Protocol protocol) 
    Creates a new HTTP connection for the given host and port using the given protocol.
    Parameters:
    host - the host to connect to
    port - the port to connect to
    protocol - the protocol to use
 public HttpConnection(String host,
    String virtualHost,
    int port,
    Protocol protocol) 
    Creates a new HTTP connection for the given host with the virtual alias and port using given protocol.
    Parameters:
    host - the host to connect to
    virtualHost - the virtual host requests will be sent to
    port - the port to connect to
    protocol - the protocol to use
 public HttpConnection(String proxyHost,
    int proxyPort,
    String host,
    int port) 
    Creates a new HTTP connection for the given host and port via the given proxy host and port using the default protocol.
    Parameters:
    proxyHost - the host to proxy via
    proxyPort - the port to proxy via
    host - the host to connect to
    port - the port to connect to
 public HttpConnection(String proxyHost,
    int proxyPort,
    String host,
    int port,
    Protocol protocol) 
    Creates a new HTTP connection for the given host with the virtual alias and port via the given proxy host and port using the given protocol.
    Parameters:
    proxyHost - the host to proxy via
    proxyPort - the port to proxy via
    host - the host to connect to. Parameter value must be non-null.
    port - the port to connect to
    protocol - The protocol to use. Parameter value must be non-null.
 public HttpConnection(String proxyHost,
    int proxyPort,
    String host,
    String virtualHost,
    int port,
    Protocol protocol) 
    Creates a new HTTP connection for the given host with the virtual alias and port via the given proxy host and port using the given protocol.
    Parameters:
    proxyHost - the host to proxy via
    proxyPort - the port to proxy via
    host - the host to connect to. Parameter value must be non-null.
    virtualHost - No longer applicable.
    port - the port to connect to
    protocol - The protocol to use. Parameter value must be non-null.
Method from org.apache.commons.httpclient.HttpConnection Summary:
assertNotOpen,   assertOpen,   close,   closeIfStale,   closeSocketAndStreams,   flushRequestOutputStream,   getHost,   getHttpConnectionManager,   getLastResponseInputStream,   getLocalAddress,   getParams,   getPort,   getProtocol,   getProxyHost,   getProxyPort,   getRequestOutputStream,   getResponseInputStream,   getSendBufferSize,   getSoTimeout,   getSocket,   getVirtualHost,   isLocked,   isOpen,   isProxied,   isResponseAvailable,   isResponseAvailable,   isSecure,   isStale,   isStaleCheckingEnabled,   isTransparent,   open,   print,   print,   printLine,   printLine,   printLine,   readLine,   readLine,   releaseConnection,   setConnectionTimeout,   setHost,   setHttpConnectionManager,   setLastResponseInputStream,   setLocalAddress,   setLocked,   setParams,   setPort,   setProtocol,   setProxyHost,   setProxyPort,   setSendBufferSize,   setSoTimeout,   setSocketTimeout,   setStaleCheckingEnabled,   setVirtualHost,   shutdownOutput,   tunnelCreated,   write,   write,   writeLine,   writeLine
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.commons.httpclient.HttpConnection Detail:
 protected  void assertNotOpen() throws IllegalStateException 
 protected  void assertOpen() throws IllegalStateException 
 public  void close() 
    Closes the socket and streams.
 public boolean closeIfStale() throws IOException 
    Closes the connection if stale.
 protected  void closeSocketAndStreams() 
    Closes everything out.
 public  void flushRequestOutputStream() throws IOException 
    Flushes the output request stream. This method should be called to ensure that data written to the request OutputStream is sent to the server.
 public String getHost() 
    Returns the host.
 public HttpConnectionManager getHttpConnectionManager() 
    Returns the httpConnectionManager.
 public InputStream getLastResponseInputStream() 
    Returns the stream used to read the last response's body.

    Clients will generally not need to call this function unless using HttpConnection directly, instead of calling HttpClient#executeMethod . For those clients, call this function, and if it returns a non-null stream, close the stream before attempting to execute a method. Note that calling "close" on the stream returned by this function may close the connection if the previous response contained a "Connection: close" header.

 public InetAddress getLocalAddress() 
    Return the local address used when creating the connection. If null, the default address is used.
 public HttpConnectionParams getParams() 
 public int getPort() 
    Returns the port of the host. If the port is -1 (or less than 0) the default port for the current protocol is returned.
 public Protocol getProtocol() 
    Returns the protocol used to establish the connection.
 public String getProxyHost() 
    Returns the proxy host.
 public int getProxyPort() 
    Returns the port of the proxy host.
 public OutputStream getRequestOutputStream() throws IOException, IllegalStateException 
 public InputStream getResponseInputStream() throws IOException, IllegalStateException 
    Return a InputStream suitable for reading the response.
 public int getSendBufferSize() throws SocketException 
    Gets the socket's sendBufferSize.
 public int getSoTimeout() throws SocketException 
Deprecated! Use - HttpConnectionParams#getSoTimeout() , HttpConnection#getParams() .

    Returns the Socket 's timeout, via Socket#getSoTimeout , if the connection is already open. If no connection is open, return the value subsequent connection will use.

    Note: This is not a connection timeout but a timeout on network traffic!

 protected Socket getSocket() 
    Returns the connection socket.
 public String getVirtualHost() 
Deprecated! no - longer applicable

    Returns the target virtual host.
 protected boolean isLocked() 
    Tests if the connection is locked. Locked connections cannot be released. An attempt to release a locked connection will have no effect.
 public boolean isOpen() 
    Tests if the connection is open.
 public boolean isProxied() 
    Returns true if the connection is established via a proxy, false otherwise.
 public boolean isResponseAvailable() throws IOException 
    Tests if input data avaialble. This method returns immediately and does not perform any read operations on the input socket
 public boolean isResponseAvailable(int timeout) throws IOException 
    Tests if input data becomes available within the given period time in milliseconds.
 public boolean isSecure() 
    Returns true if the connection is established over a secure protocol.
 protected boolean isStale() throws IOException 
    Determines whether this connection is "stale", which is to say that either it is no longer open, or an attempt to read the connection would fail.

    Unfortunately, due to the limitations of the JREs prior to 1.4, it is not possible to test a connection to see if both the read and write channels are open - except by reading and writing. This leads to a difficulty when some connections leave the "write" channel open, but close the read channel and ignore the request. This function attempts to ameliorate that problem by doing a test read, assuming that the caller will be doing a write followed by a read, rather than the other way around.

    To avoid side-effects, the underlying connection is wrapped by a BufferedInputStream , so although data might be read, what is visible to clients of the connection will not change with this call.

 public boolean isStaleCheckingEnabled() 
Deprecated! Use - HttpConnectionParams#isStaleCheckingEnabled() , HttpConnection#getParams() .

    Tests if stale checking is enabled.
 public boolean isTransparent() 
    Indicates if the connection is completely transparent from end to end.
 public  void open() throws IOException 
    Establishes a connection to the specified host and port (via a proxy if specified). The underlying socket is created from the ProtocolSocketFactory .
 public  void print(String data) throws IOException, IllegalStateException 
Deprecated! Use - #print(String, String) Writes the specified String (as bytes) to the output stream.

 public  void print(String data,
    String charset) throws IOException, IllegalStateException 
    Writes the specified String (as bytes) to the output stream.
 public  void printLine() throws IOException, IllegalStateException 
    Writes "\r\n".getBytes() to the output stream.
 public  void printLine(String data) throws IOException, IllegalStateException 
Deprecated! Use - #printLine(String, String) Writes the specified String (as bytes), followed by "\r\n".getBytes() to the output stream.

 public  void printLine(String data,
    String charset) throws IOException, IllegalStateException 
    Writes the specified String (as bytes), followed by "\r\n".getBytes() to the output stream.
 public String readLine() throws IOException, IllegalStateException 
Deprecated! use - #readLine(String)

    Reads up to "\n" 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.
 public String readLine(String charset) throws IOException, IllegalStateException 
    Reads up to "\n" 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.
 public  void releaseConnection() 
    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 setConnectionTimeout(int timeout) 
Deprecated! Use - HttpConnectionParams#setConnectionTimeout(int) , HttpConnection#getParams() .

    Sets the connection timeout. This is the maximum time that may be spent until a connection is established. The connection will fail after this amount of time.
 public  void setHost(String host) throws IllegalStateException 
    Sets the host to connect to.
 public  void setHttpConnectionManager(HttpConnectionManager httpConnectionManager) 
    Sets the httpConnectionManager.
 public  void setLastResponseInputStream(InputStream inStream) 
    Set the state to keep track of the last response for the last request.

    The connection managers use this to ensure that previous requests are properly closed before a new request is attempted. That way, a GET request need not be read in its entirety before a new request is issued. Instead, this stream can be closed as appropriate.

 public  void setLocalAddress(InetAddress localAddress) 
    Set the local address used when creating the connection. If unset or null, the default address is used.
 protected  void setLocked(boolean locked) 
    Locks or unlocks the connection. Locked connections cannot be released. An attempt to release a locked connection will have no effect.
 public  void setParams(HttpConnectionParams params) 
 public  void setPort(int port) throws IllegalStateException 
    Sets the port to connect to.
 public  void setProtocol(Protocol protocol) 
    Sets the protocol used to establish the connection
 public  void setProxyHost(String host) throws IllegalStateException 
    Sets the host to proxy through.
 public  void setProxyPort(int port) throws IllegalStateException 
    Sets the port of the host to proxy through.
 public  void setSendBufferSize(int sendBufferSize) throws SocketException 
Deprecated! Use - HttpConnectionParams#setSendBufferSize(int) , HttpConnection#getParams() .

    Sets the socket's sendBufferSize.
 public  void setSoTimeout(int timeout) throws SocketException, IllegalStateException 
Deprecated! Use - HttpConnectionParams#setSoTimeout(int) , HttpConnection#getParams() .

    Set the Socket 's timeout, via Socket#setSoTimeout . If the connection is already open, the SO_TIMEOUT is changed. If no connection is open, then subsequent connections will use the timeout value.

    Note: This is not a connection timeout but a timeout on network traffic!

 public  void setSocketTimeout(int timeout) throws SocketException, IllegalStateException 
    Sets SO_TIMEOUT value directly on the underlying socket . This method does not change the default read timeout value set via HttpConnectionParams .
 public  void setStaleCheckingEnabled(boolean staleCheckEnabled) 
Deprecated! Use - HttpConnectionParams#setStaleCheckingEnabled(boolean) , HttpConnection#getParams() .

    Sets whether or not isStale() will be called when testing if this connection is open.

    Setting this flag to false will increase performance when reusing connections, but it will also make them less reliable. Stale checking ensures that connections are viable before they are used. When set to false some method executions will result in IOExceptions and they will have to be retried.

 public  void setVirtualHost(String host) throws IllegalStateException 
Deprecated! no - longer applicable

    Sets the virtual host to target.
 public  void shutdownOutput() 
Deprecated! unused -

    Attempts to shutdown the Socket 's output, via Socket.shutdownOutput() when running on JVM 1.3 or higher.
 public  void tunnelCreated() throws IOException, IllegalStateException 
    Instructs the proxy to establish a secure tunnel to the host. The socket will be switched to the secure socket. Subsequent communication is done via the secure socket. The method can only be called once on a proxied secure connection.
 public  void write(byte[] data) throws IOException, IllegalStateException 
    Writes the specified bytes to the output stream.
 public  void write(byte[] data,
    int offset,
    int length) throws IOException, IllegalStateException 
    Writes length bytes in data starting at offset to the output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.
 public  void writeLine() throws IOException, IllegalStateException 
    Writes "\r\n".getBytes() to the output stream.
 public  void writeLine(byte[] data) throws IOException, IllegalStateException 
    Writes the specified bytes, followed by "\r\n".getBytes() to the output stream.