org.apache.commons.httpclient

Class HttpConnection


public class HttpConnection
extends Object

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
Socket.setTcpNoDelay(boolean) SO_NODELAY HttpConnectionParams.setTcpNoDelay(boolean)
Socket.setSoTimeout(int) SO_TIMEOUT HttpConnectionParams.setSoTimeout(int)
Socket.setSendBufferSize(int) SO_SNDBUF HttpConnectionParams.setSendBufferSize(int)
Socket.setReceiveBufferSize(int) SO_RCVBUF HttpConnectionParams.setReceiveBufferSize(int)

Field Summary

protected boolean
isOpen
Whether or not the connection is connected.

Constructor Summary

HttpConnection(String host, int port)
Creates a new HTTP connection for the given host and port.
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.
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.
HttpConnection(String proxyHost, int proxyPort, String host, String virtualHost, int port, Protocol protocol)
Deprecated. use #HttpConnection(String, int, String, int, Protocol)
HttpConnection(String host, int port, Protocol protocol)
Creates a new HTTP connection for the given host and port using the given protocol.
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.
HttpConnection(HostConfiguration hostConfiguration)
Creates a new HTTP connection for the given host configuration.

Method Summary

protected void
assertNotOpen()
Throws an IllegalStateException if the connection is already open.
protected void
assertOpen()
Throws an IllegalStateException if the connection is not open.
void
close()
Closes the socket and streams.
boolean
closeIfStale()
Closes the connection if stale.
protected void
closeSocketAndStreams()
Closes everything out.
void
flushRequestOutputStream()
Flushes the output request stream.
String
getHost()
Returns the host.
HttpConnectionManager
getHttpConnectionManager()
Returns the httpConnectionManager.
InputStream
getLastResponseInputStream()
Returns the stream used to read the last response's body.
InetAddress
getLocalAddress()
Return the local address used when creating the connection.
HttpConnectionParams
getParams()
Returns HTTP protocol parameters associated with this method.
int
getPort()
Returns the port of the host.
Protocol
getProtocol()
Returns the protocol used to establish the connection.
String
getProxyHost()
Returns the proxy host.
int
getProxyPort()
Returns the port of the proxy host.
OutputStream
getRequestOutputStream()
Returns an OutputStream suitable for writing the request.
InputStream
getResponseInputStream()
Return a InputStream suitable for reading the response.
int
getSendBufferSize()
Gets the socket's sendBufferSize.
int
getSoTimeout()
Deprecated. Use HttpConnectionParams.getSoTimeout(), getParams().
protected Socket
getSocket()
Returns the connection socket.
String
getVirtualHost()
Deprecated. no longer applicable
protected boolean
isLocked()
Tests if the connection is locked.
boolean
isOpen()
Tests if the connection is open.
boolean
isProxied()
Returns true if the connection is established via a proxy, false otherwise.
boolean
isResponseAvailable()
Tests if input data avaialble.
boolean
isResponseAvailable(int timeout)
Tests if input data becomes available within the given period time in milliseconds.
boolean
isSecure()
Returns true if the connection is established over a secure protocol.
protected boolean
isStale()
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.
boolean
isStaleCheckingEnabled()
Deprecated. Use HttpConnectionParams.isStaleCheckingEnabled(), getParams().
boolean
isTransparent()
Indicates if the connection is completely transparent from end to end.
void
open()
Establishes a connection to the specified host and port (via a proxy if specified).
void
print(String data)
Deprecated. Use print(String,String) Writes the specified String (as bytes) to the output stream.
void
print(String data, String charset)
Writes the specified String (as bytes) to the output stream.
void
printLine()
Writes "\r\n".getBytes() to the output stream.
void
printLine(String data)
Deprecated. Use printLine(String,String) Writes the specified String (as bytes), followed by "\r\n".getBytes() to the output stream.
void
printLine(String data, String charset)
Writes the specified String (as bytes), followed by "\r\n".getBytes() to the output stream.
String
readLine()
Deprecated. use #readLine(String)
String
readLine(String charset)
Reads up to "\n" from the (unchunked) input stream.
void
releaseConnection()
Releases the connection.
void
setConnectionTimeout(int timeout)
Deprecated. Use HttpConnectionParams.setConnectionTimeout(int), getParams().
void
setHost(String host)
Sets the host to connect to.
void
setHttpConnectionManager(HttpConnectionManager httpConnectionManager)
Sets the httpConnectionManager.
void
setLastResponseInputStream(InputStream inStream)
Set the state to keep track of the last response for the last request.
void
setLocalAddress(InetAddress localAddress)
Set the local address used when creating the connection.
protected void
setLocked(boolean locked)
Locks or unlocks the connection.
void
setParams(HttpConnectionParams params)
Assigns HTTP protocol parameters for this method.
void
setPort(int port)
Sets the port to connect to.
void
setProtocol(Protocol protocol)
Sets the protocol used to establish the connection
void
setProxyHost(String host)
Sets the host to proxy through.
void
setProxyPort(int port)
Sets the port of the host to proxy through.
void
setSendBufferSize(int sendBufferSize)
Deprecated. Use HttpConnectionParams.setSendBufferSize(int), getParams().
void
setSoTimeout(int timeout)
Deprecated. Use HttpConnectionParams.setSoTimeout(int), getParams().
void
setSocketTimeout(int timeout)
Sets SO_TIMEOUT value directly on the underlying socket.
void
setStaleCheckingEnabled(boolean staleCheckEnabled)
Deprecated. Use HttpConnectionParams.setStaleCheckingEnabled(boolean), getParams().
void
setVirtualHost(String host)
Deprecated. no longer applicable
void
shutdownOutput()
Deprecated. unused
void
tunnelCreated()
Instructs the proxy to establish a secure tunnel to the host.
void
write(byte[] data)
Writes the specified bytes to the output stream.
void
write(byte[] data, int offset, int length)
Writes length bytes in data starting at offset to the output stream.
void
writeLine()
Writes "\r\n".getBytes() to the output stream.
void
writeLine(byte[] data)
Writes the specified bytes, followed by "\r\n".getBytes() to the output stream.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

isOpen

protected boolean isOpen
Whether or not the connection is connected.

Constructor Details

HttpConnection

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
Usages and Demos :

View More Examples of HttpConnection(String host,int port)
   1:             hostInNonProxyList) {
   2:             return new HttpConnection(host, port);
   3:         } else {
   4:             if (tcp.getProxyUser().length() != 0) {
   5:                 Credentials proxyCred =

View Full Code Here

HttpConnection

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

HttpConnection

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.

HttpConnection

public HttpConnection(String proxyHost,
                      int proxyPort,
                      String host,
                      String virtualHost,
                      int port,
                      Protocol protocol)

Deprecated. use #HttpConnection(String, int, String, int, 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.

HttpConnection

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
Usages and Demos :

View More Examples of HttpConnection(String host,int port,Protocol protocol)
   1:         if (tcp.getProxyHost().length() == 0 || hostInNonProxyList) {
   2:             return new HttpConnection(host, port, true);
   3:         } else {
   4: 
   5:             int tunnelPort = ((tcp.getProxyPort().length() != 0)

View Full Code Here

HttpConnection

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

HttpConnection

public HttpConnection(HostConfiguration hostConfiguration)
Creates a new HTTP connection for the given host configuration.
Parameters:
hostConfiguration - the host/proxy/protocol to use
Usages and Demos :

View More Examples of HttpConnection(HostConfiguration hostConfiguration)
   1: 
   2:         HttpConnection conn = new HttpConnection(hostConfiguration);
   3:         conn.setHttpConnectionManager(this);
   4:         conn.getParams().setDefaults(this.getParams());
   5:         return conn;

View Full Code Here
   1: 
   2:             httpConnection = new HttpConnection(hostConfiguration);
   3:             httpConnection.setHttpConnectionManager(this);
   4:             httpConnection.getParams().setDefaults(this.params);
   5:             ci.conn = httpConnection;

View Full Code Here
   1: 
   2:             return new HttpConnection(tcp.getProxyHost(), tunnelPort, host, port, true);
   3:         }
   4:         ...
   5:             }
   6:             return new HttpConnection(tcp.getProxyHost(),
   7:                                       new Integer(tcp.getProxyPort()).intValue(), host,
   8:                                       port);
   9:         }

View Full Code Here

Method Details

assertNotOpen

protected void assertNotOpen()
            throws IllegalStateException
Throws an IllegalStateException if the connection is already open.
Throws:
IllegalStateException - if connected

assertOpen

protected void assertOpen()
            throws IllegalStateException
Throws an IllegalStateException if the connection is not open.
Throws:
IllegalStateException - if not connected

close

public void close()
Closes the socket and streams.
Usages and Demos :

View More Examples of close()
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.SimpleHttpConnectionManager;
   4:         ...
   5: 
   6:     public HttpConnection getConnectionWithTimeout(
   7:         HostConfiguration hostConfiguration, long timeout) {
   8:         ...
   9: 
  10:         HttpConnection conn = new HttpConnection(hostConfiguration);
  11:         conn.setHttpConnectionManager(this);
  12:         ...
  13:     public void releaseConnection(HttpConnection conn) {
  14:         conn.close();

View Full Code Here
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.HttpConnectionManager;
   4:         ...
   5: 
   6:     private static boolean finishLastResponse(final HttpConnection conn) {
   7:         InputStream lastResponse = conn.getLastResponseInputStream();
   8:         ...
   9:             try {
  10:                 lastResponse.close();
  11:                 return true;
  12:         ...
  13: 
  14:     public HttpConnection getConnection(

View Full Code Here

closeIfStale

public boolean closeIfStale()
            throws IOException
Closes the connection if stale.
Returns:
true if the connection was stale and therefore closed, false otherwise.
Since:
3.0
See Also:
isStale()

closeSocketAndStreams

protected void closeSocketAndStreams()
Closes everything out.

flushRequestOutputStream

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.
Throws:
IOException - if an I/O problem occurs

getHost

public String getHost()
Returns the host.
Returns:
the host.

getHttpConnectionManager

public HttpConnectionManager getHttpConnectionManager()
Returns the httpConnectionManager.
Returns:
HttpConnectionManager

getLastResponseInputStream

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.

Returns:
An InputStream corresponding to the body of the last response.

getLocalAddress

public InetAddress getLocalAddress()
Return the local address used when creating the connection. If null, the default address is used.
Returns:
InetAddress the local address to be used when creating Sockets

getParams

public HttpConnectionParams getParams()
Returns HTTP protocol parameters associated with this method.
Returns:
HTTP parameters.
Since:
3.0
Usages and Demos :

View More Examples of getParams()
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.SimpleHttpConnectionManager;
   4:         ...
   5: 
   6:     public HttpConnection getConnectionWithTimeout(
   7:         HostConfiguration hostConfiguration, long timeout) {
   8:         ...
   9: 
  10:         HttpConnection conn = new HttpConnection(hostConfiguration);
  11:         conn.setHttpConnectionManager(this);
  12:         ...
  13:         conn.getParams().setDefaults(this.getParams());

View Full Code Here

getPort

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.
Returns:
the port.

getProtocol

public Protocol getProtocol()
Returns the protocol used to establish the connection.
Returns:
The protocol

getProxyHost

public String getProxyHost()
Returns the proxy host.
Returns:
the proxy host.

getProxyPort

public int getProxyPort()
Returns the port of the proxy host.
Returns:
the proxy port.

getRequestOutputStream

public OutputStream getRequestOutputStream()
            throws IOException,
                   IllegalStateException
Returns an OutputStream suitable for writing the request.
Returns:
a stream to write the request to
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs

getResponseInputStream

public InputStream getResponseInputStream()
            throws IOException,
                   IllegalStateException
Return a InputStream suitable for reading the response.
Returns:
InputStream The response input stream.
Throws:
IOException - If an IO problem occurs
IllegalStateException - If the connection isn't open.

getSendBufferSize

public int getSendBufferSize()
            throws SocketException
Gets the socket's sendBufferSize.
Returns:
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

getSoTimeout

public int getSoTimeout()
            throws SocketException

Deprecated. Use HttpConnectionParams.getSoTimeout(), 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!

Returns:
the timeout value

getSocket

protected Socket getSocket()
Returns the connection socket.
Returns:
the socket.
Since:
3.0

getVirtualHost

public String getVirtualHost()

Deprecated. no longer applicable

Returns the target virtual host.
Returns:
the virtual host.

isLocked

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.
Returns:
true if the connection is locked, false otherwise.
Since:
3.0

isOpen

public boolean isOpen()
Tests if the connection is open.
Returns:
true if the connection is open

isProxied

public boolean isProxied()
Returns true if the connection is established via a proxy, false otherwise.
Returns:
true if a proxy is used to establish the connection, false otherwise.

isResponseAvailable

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
Returns:
boolean true if input data is available, false otherwise.
Throws:
IOException - If an IO problem occurs
IllegalStateException - If the connection isn't open.

isResponseAvailable

public boolean isResponseAvailable(int timeout)
            throws IOException
Tests if input data becomes available within the given period time in milliseconds.
Parameters:
timeout - The number milliseconds to wait for input data to become available
Returns:
boolean true if input data is availble, false otherwise.
Throws:
IOException - If an IO problem occurs
IllegalStateException - If the connection isn't open.
Usages and Demos :

View More Examples of isResponseAvailable(int timeout)
   1: 
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.HttpException;
   4:         ...
   5: 
   6:     protected void readResponseBody(HttpState state, HttpConnection conn)
   7:     throws HttpException, IOException {
   8:         ...
   9:         LOG.trace(
  10:             "enter HeadMethod.readResponseBody(HttpState, HttpConnection)");
  11:         
  12:         ...
  13:             try {
  14:                 responseAvailable = conn.isResponseAvailable(bodyCheckTimeout);

View Full Code Here

isSecure

public boolean isSecure()
Returns true if the connection is established over a secure protocol.
Returns:
true if connected over a secure protocol.

isStale

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.

Returns:
true if the connection is already closed, or a read would fail.
Throws:
IOException - if the stale connection test is interrupted.

isStaleCheckingEnabled

public boolean isStaleCheckingEnabled()

Deprecated. Use HttpConnectionParams.isStaleCheckingEnabled(), getParams().

Tests if stale checking is enabled.
Returns:
true if enabled
See Also:
isStale()

isTransparent

public boolean isTransparent()
Indicates if the connection is completely transparent from end to end.
Returns:
true if conncetion is not proxied or tunneled through a transparent proxy; false otherwise.

open

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.
Throws:
IOException - if an attempt to establish the connection results in an I/O error.

print

public void print(String data)
            throws IOException,
                   IllegalStateException

Deprecated. Use print(String,String) Writes the specified String (as bytes) to the output stream.

Parameters:
data - the string to be written
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs

print

public void print(String data,
                  String charset)
            throws IOException,
                   IllegalStateException
Writes the specified String (as bytes) to the output stream.
Parameters:
data - the string to be written
charset - the charset to use for writing the data
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs
Since:
3.0

printLine

public void printLine()
            throws IOException,
                   IllegalStateException
Writes "\r\n".getBytes() to the output stream.
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs

printLine

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.

Parameters:
data - the data to be written
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs

printLine

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.
Parameters:
data - the data to be written
charset - the charset to use for writing the data
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs
Since:
3.0

readLine

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.
Returns:
a line from the response
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs

readLine

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.
Parameters:
charset - the charset to use for reading the data
Returns:
a line from the response
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs
Since:
3.0

releaseConnection

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.

setConnectionTimeout

public void setConnectionTimeout(int timeout)

Deprecated. Use HttpConnectionParams.setConnectionTimeout(int), 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.
Parameters:
timeout - The timeout in milliseconds. 0 means timeout is not used.

setHost

public void setHost(String host)
            throws IllegalStateException
Sets the host to connect to.
Parameters:
host - the host to connect to. Parameter value must be non-null.
Throws:
IllegalStateException - if the connection is already open

setHttpConnectionManager

public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager)
Sets the httpConnectionManager.
Parameters:
httpConnectionManager - The httpConnectionManager to set
Usages and Demos :

View More Examples of setHttpConnectionManager(HttpConnectionManager httpConnectionManager)
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.SimpleHttpConnectionManager;
   4:         ...
   5: 
   6:     public HttpConnection getConnectionWithTimeout(
   7:         HostConfiguration hostConfiguration, long timeout) {
   8:         ...
   9: 
  10:         HttpConnection conn = new HttpConnection(hostConfiguration);
  11:         ...
  12:         conn.setHttpConnectionManager(this);

View Full Code Here
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.HttpConnectionManager;
   4:         ...
   5: 
   6:     private static boolean finishLastResponse(final HttpConnection conn) {
   7:         InputStream lastResponse = conn.getLastResponseInputStream();
   8:         ...
   9: 
  10:     public HttpConnection getConnection(
  11:         final HostConfiguration hostConfiguration) {
  12:         ...
  13:             httpConnection = new HttpConnection(hostConfiguration);
  14:             httpConnection.setHttpConnectionManager(this);

View Full Code Here

setLastResponseInputStream

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.

Parameters:
inStream - The stream associated with an HttpMethod.
Usages and Demos :

View More Examples of setLastResponseInputStream(InputStream inStream)
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.SimpleHttpConnectionManager;
   4:         ...
   5: 
   6:     public HttpConnection getConnectionWithTimeout(
   7:         HostConfiguration hostConfiguration, long timeout) {
   8:         ...
   9: 
  10:         HttpConnection conn = new HttpConnection(hostConfiguration);
  11:         conn.setHttpConnectionManager(this);
  12:         ...
  13:         if (lastResponse != null) {
  14:             conn.setLastResponseInputStream(null);

View Full Code Here
   1: import org.apache.commons.httpclient.HostConfiguration;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.HttpConnectionManager;
   4:         ...
   5: 
   6:     private static boolean finishLastResponse(final HttpConnection conn) {
   7:         InputStream lastResponse = conn.getLastResponseInputStream();
   8:         ...
   9:         if(lastResponse != null) {
  10:             conn.setLastResponseInputStream(null);
  11:             try {
  12:         ...
  13: 
  14:     public HttpConnection getConnection(

View Full Code Here

setLocalAddress

public void setLocalAddress(InetAddress localAddress)
Set the local address used when creating the connection. If unset or null, the default address is used.
Parameters:
localAddress - the local address to use

setLocked

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.
Parameters:
locked - true to lock the connection, false to unlock the connection.
Since:
3.0

setParams

public void setParams(HttpConnectionParams params)
Assigns HTTP protocol parameters for this method.
Since:
3.0

setPort

public void setPort(int port)
            throws IllegalStateException
Sets the port to connect to.
Parameters:
port - the port to connect to
Throws:
IllegalStateException - if the connection is already open

setProtocol

public void setProtocol(Protocol protocol)
Sets the protocol used to establish the connection
Parameters:
protocol - The protocol to use.
Throws:
IllegalStateException - if the connection is already open

setProxyHost

public void setProxyHost(String host)
            throws IllegalStateException
Sets the host to proxy through.
Parameters:
host - the host to proxy through.
Throws:
IllegalStateException - if the connection is already open

setProxyPort

public void setProxyPort(int port)
            throws IllegalStateException
Sets the port of the host to proxy through.
Parameters:
port - the port of the host to proxy through.
Throws:
IllegalStateException - if the connection is already open

setSendBufferSize

public void setSendBufferSize(int sendBufferSize)
            throws SocketException

Deprecated. Use HttpConnectionParams.setSendBufferSize(int), getParams().

Sets the socket's sendBufferSize.
Parameters:
sendBufferSize - the size to set for the socket OutputStream
Throws:
SocketException - if an error occurs while setting the socket value

setSoTimeout

public void setSoTimeout(int timeout)
            throws SocketException,
                   IllegalStateException

Deprecated. Use HttpConnectionParams.setSoTimeout(int), getParams().

Set the Socket's timeout, via Socket.setSoTimeout(int). 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!

Parameters:
timeout - the timeout value
Throws:
SocketException - - if there is an error in the underlying protocol, such as a TCP error.
Usages and Demos :

View More Examples of setSoTimeout(int timeout)
   1: import org.apache.commons.httpclient.Header;
   2: import org.apache.commons.httpclient.HttpConnection;
   3: import org.apache.commons.httpclient.HttpState;
   4:         ...
   5:             int port = targetURL.getPort();
   6:             HttpConnection conn = null;
   7:             HttpState state = new HttpState();
   8:         ...
   9: 
  10:     private HttpConnection getSecureConnection(
  11:             HttpState state, String host, int port) throws Exception {
  12:         ...
  13:         if (msgContext.getTimeout() != 0) {
  14:             conn.setSoTimeout(msgContext.getTimeout());

View Full Code Here

setSocketTimeout

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.
Parameters:
timeout - the timeout value
Throws:
SocketException - - if there is an error in the underlying protocol, such as a TCP error.
IllegalStateException - if not connected
Since:
3.0

setStaleCheckingEnabled

public void setStaleCheckingEnabled(boolean staleCheckEnabled)

Deprecated. Use HttpConnectionParams.setStaleCheckingEnabled(boolean), 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.

Parameters:
staleCheckEnabled - true to enable isStale()

setVirtualHost

public void setVirtualHost(String host)
            throws IllegalStateException

Deprecated. no longer applicable

Sets the virtual host to target.
Parameters:
host - the virtual host name that should be used instead of physical host name when sending HTTP requests. Virtual host name can be set to null if virtual host name is not to be used
Throws:
IllegalStateException - if the connection is already open

shutdownOutput

public void shutdownOutput()

Deprecated. unused

Attempts to shutdown the Socket's output, via Socket.shutdownOutput() when running on JVM 1.3 or higher.

tunnelCreated

public void tunnelCreated()
            throws IllegalStateException,
                   IOException
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.
Throws:
IllegalStateException - if connection is not secure and proxied or if the socket is already secure.
IOException - if an attempt to establish the secure tunnel results in an I/O error.

write

public void write(byte[] data)
            throws IOException,
                   IllegalStateException
Writes the specified bytes to the output stream.
Parameters:
data - the data to be written
Throws:
IllegalStateException - if not connected
IOException - if an I/O problem occurs

write

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.
Parameters:
data - array containing the data to be written.
offset - the start offset in the data.
length - the number of bytes to write.
Throws:
IllegalStateException - if not connected
IOException - if an I/O problem occurs

writeLine

public void writeLine()
            throws IOException,
                   IllegalStateException
Writes "\r\n".getBytes() to the output stream.
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs

writeLine

public void writeLine(byte[] data)
            throws IOException,
                   IllegalStateException
Writes the specified bytes, followed by "\r\n".getBytes() to the output stream.
Parameters:
data - the bytes to be written
Throws:
IllegalStateException - if the connection is not open
IOException - if an I/O problem occurs