|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.httpclient.HttpConnection
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 | None |
Socket.setSoTimeout(int)
| SO_TIMEOUT | setConnectionTimeout(int)
|
Nested Class Summary | |
class |
HttpConnection.ConnectionTimeoutException
Signals that a timeout occured while opening the socket. |
Constructor Summary | |
HttpConnection(HostConfiguration hostConfiguration)
Creates a new HttpConnection. |
|
HttpConnection(String host,
int port)
Constructor. |
|
HttpConnection(String host,
int port,
boolean secure)
Deprecated. use HttpConnection(String, int, Protocol) |
|
HttpConnection(String host,
int port,
Protocol protocol)
Constructor. |
|
HttpConnection(String proxyHost,
int proxyPort,
String host,
int port)
Constructor. |
|
HttpConnection(String proxyHost,
int proxyPort,
String host,
int port,
boolean secure)
Deprecated. use HttpConnection(String, int, String, int, Protocol) |
|
HttpConnection(String proxyHost,
int proxyPort,
String host,
int port,
Protocol protocol)
Create an instance |
Method Summary | |
protected void |
assertNotOpen()
Throw an IllegalStateException if I am connected. |
protected void |
assertOpen()
Throw an IllegalStateException if I am not connected. |
void |
close()
Close my socket and streams. |
protected void |
closeSocketAndStreams()
Close everything out. |
String |
getHost()
Return my host. |
HttpConnectionManager |
getHttpConnectionManager()
Returns the httpConnectionManager. |
InputStream |
getLastResponseInputStream()
Returns the stream used to read the last response's body. |
int |
getPort()
Return my port. |
Protocol |
getProtocol()
Get the protocol. |
String |
getProxyHost()
Return my proxy host. |
int |
getProxyPort()
Return my proxy port. |
OutputStream |
getRequestOutputStream()
Return a RequestOutputStream suitable for writing (possibly
chunked) bytes to my OutputStream . |
OutputStream |
getRequestOutputStream(boolean useChunking)
Deprecated. Use new ChunkedOutputStream(httpConnecion.getRequestOutputStream()); |
InputStream |
getResponseInputStream()
Return the response input stream |
InputStream |
getResponseInputStream(HttpMethod method)
Deprecated. Use getResponseInputStream() instead. |
boolean |
isOpen()
Return true if I am connected, false otherwise. |
boolean |
isProxied()
Return true if I am (or I will be) connected via a proxy, false otherwise. |
boolean |
isResponseAvaliable()
Tests if input data avaialble. |
boolean |
isSecure()
Return true if I will (or I am) connected over a secure (HTTPS/SSL) protocol. |
boolean |
isTransparent()
Indicates if the connection is completely transparent from end to end. |
void |
open()
Open this connection to the current host and port (via a proxy if so configured). |
void |
print(String data)
Write the specified String (as bytes) to my output stream. |
void |
printLine()
Write "\r\n".getBytes() to my output stream. |
void |
printLine(String data)
Write the specified String (as bytes), followed by "\r\n".getBytes() to my output stream. |
String |
readLine()
Read up to "\r\n" from my (unchunked) input stream. |
void |
releaseConnection()
Release the connection. |
void |
setConnectionTimeout(int timeout)
Sets the connection timeout. |
void |
setHost(String host)
Set my host. |
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 |
setPort(int port)
Set my port. |
void |
setProtocol(Protocol protocol)
Sets the protocol used by this connection. |
void |
setProxyHost(String host)
Set the host I should proxy through. |
void |
setProxyPort(int port)
Set the port I should proxy through. |
void |
setSecure(boolean secure)
Deprecated. use setProtocol(Protocol) |
void |
setSoTimeout(int timeout)
Set my Socket 's timeout, via Socket.setSoTimeout(int) . |
void |
shutdownOutput()
Shutdown my Socket 's output, via Socket.shutdownOutput() . |
void |
tunnelCreated()
Calling this method indicates that the proxy has successfully created the tunnel to the host. |
boolean |
waitForResponse(long timeout_ms)
Waits for the specified number of milliseconds for input data to become available |
void |
write(byte[] data)
Write the specified bytes to my output stream. |
void |
write(byte[] data,
int offset,
int length)
Write length bytes in data starting at offset to my output stream. |
void |
writeLine()
Write "\r\n".getBytes() to my output stream. |
void |
writeLine(byte[] data)
Write the specified bytes, followed by "\r\n".getBytes() to my output stream. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public HttpConnection(String host, int port)
host
- the host I should connect toport
- the port I should connect topublic HttpConnection(String host, int port, boolean secure)
host
- the host I should connect toport
- the port I should connect tosecure
- when true, connect via HTTPS (SSL)HttpConnection(String,int,Protocol)
public HttpConnection(String host, int port, Protocol protocol)
host
- the host I should connect toport
- the port I should connect toprotocol
- the protocol to usepublic HttpConnection(String proxyHost, int proxyPort, String host, int port)
proxyHost
- the host I should proxy viaproxyPort
- the port I should proxy viahost
- the host I should connect toport
- the port I should connect topublic HttpConnection(String proxyHost, int proxyPort, String host, int port, boolean secure)
proxyHost
- the host I should proxy viaproxyPort
- the port I should proxy viahost
- the host I should connect to. Parameter value must be non-null.port
- the port I should connect tosecure
- when true, connect via HTTPS (SSL)HttpConnection(String, int, String, int, Protocol)
public HttpConnection(HostConfiguration hostConfiguration)
hostConfiguration
- the host/proxy/protocol to usepublic HttpConnection(String proxyHost, int proxyPort, String host, int port, Protocol protocol)
proxyHost
- the host I should proxy viaproxyPort
- the port I should proxy viahost
- the host I should connect to. Parameter value must be non-null.port
- the port I should connect toprotocol
- The protocol to use.Method Detail |
public String getHost()
public void setHost(String host) throws IllegalStateException
host
- the host I should connect to. Parameter value must be non-null.
IllegalStateException
- if I am already connectedpublic int getPort()
public void setPort(int port) throws IllegalStateException
port
- the port I should connect to
IllegalStateException
- if I am already connectedpublic String getProxyHost()
public void setProxyHost(String host) throws IllegalStateException
host
- the host I should proxy through.
IllegalStateException
- if I am already connectedpublic int getProxyPort()
public void setProxyPort(int port) throws IllegalStateException
port
- the host I should proxy through.
IllegalStateException
- if I am already connectedpublic boolean isSecure()
public Protocol getProtocol()
public void setSecure(boolean secure) throws IllegalStateException
secure
- whether or not I should connect over HTTPS (SSL).
IllegalStateException
- if I am already connectedsetProtocol(Protocol)
public void setProtocol(Protocol protocol)
protocol
- The new protocol.public boolean isOpen()
public boolean isProxied()
public void setLastResponseInputStream(InputStream inStream)
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.
inStream
- The stream associated with an HttpMethod.public InputStream getLastResponseInputStream()
Clients will generally not need to call this function unless
using HttpConnection directly, instead of calling HttpClient.executeMethod(org.apache.commons.httpclient.HttpMethod)
.
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.
InputStream
corresponding to the body of the last
response.public void setSoTimeout(int timeout) throws SocketException, IllegalStateException
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!
timeout
- the timeout value
SocketException
- - if there is an error in the underlying
protocol, such as a TCP error.
IllegalStateException
- if I am not connectedpublic void setConnectionTimeout(int timeout)
timeout
- The timeout in milliseconds. 0 means timeout is not used.public void open() throws IOException
ProtocolSocketFactory
.
IOException
- when there are errors opening the connectionpublic void tunnelCreated() throws IllegalStateException, IOException
IllegalStateException
- if connection is not secure and proxied or
if the socket is already secure.
IOException
- if an error occured creating the secure socketpublic boolean isTransparent()
public OutputStream getRequestOutputStream() throws IOException, IllegalStateException
RequestOutputStream
suitable for writing (possibly
chunked) bytes to my OutputStream
.
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic OutputStream getRequestOutputStream(boolean useChunking) throws IOException, IllegalStateException
RequestOutputStream
suitable for writing (possibly
chunked) bytes to my OutputStream
.
useChunking
- when true the chunked transfer-encoding will
be used
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic InputStream getResponseInputStream(HttpMethod method) throws IOException, IllegalStateException
ResponseInputStream
suitable for reading (possibly
chunked) bytes from my InputStream
.
If the given HttpMethod
contains
a Transfer-Encoding: chunked header,
the returned stream will be configured
to read chunked bytes.
method
- This argument is ignored.
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic InputStream getResponseInputStream() throws IOException, IllegalStateException
IOException
- If an IO problem occurs
IllegalStateException
- If the connection isn't open.public boolean isResponseAvaliable() throws IOException
IOException
- If an IO problem occurs
IllegalStateException
- If the connection isn't open.public boolean waitForResponse(long timeout_ms) throws IOException, IllegalStateException
timeout_ms
- Number of milliseconds to wait for input data.
IOException
- If an IO problem occurs
IllegalStateException
- If the connection isn't open.public void write(byte[] data) throws IOException, IllegalStateException, HttpRecoverableException
data
- the data to be written
HttpRecoverableException
- if a SocketException occurs
IllegalStateException
- if not connected
IOException
- if an I/O problem occurswrite(byte[],int,int)
public void write(byte[] data, int offset, int length) throws IOException, IllegalStateException, HttpRecoverableException
data
- array containing the data to be written.offset
- the start offset in the data.length
- the number of bytes to write.
HttpRecoverableException
- if a SocketException occurs
IllegalStateException
- if not connected
IOException
- if an I/O problem occurspublic void writeLine(byte[] data) throws IOException, IllegalStateException, HttpRecoverableException
data
- the bytes to be written
HttpRecoverableException
- when socket exceptions occur writing data
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic void writeLine() throws IOException, IllegalStateException, HttpRecoverableException
HttpRecoverableException
- when socket exceptions occur writing
data
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic void print(String data) throws IOException, IllegalStateException, HttpRecoverableException
data
- the string to be written
HttpRecoverableException
- when socket exceptions occur writing
data
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic void printLine(String data) throws IOException, IllegalStateException, HttpRecoverableException
data
- the data to be written
HttpRecoverableException
- when socket exceptions occur writing
data
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic void printLine() throws IOException, IllegalStateException, HttpRecoverableException
HttpRecoverableException
- when socket exceptions occur writing
data
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic String readLine() throws IOException, IllegalStateException
IllegalStateException
- if I am not connected
IOException
- if an I/O problem occurspublic void shutdownOutput()
Socket
's output, via Socket.shutdownOutput()
.
public void close()
public HttpConnectionManager getHttpConnectionManager()
public void setHttpConnectionManager(HttpConnectionManager httpConnectionManager)
httpConnectionManager
- The httpConnectionManager to setpublic void releaseConnection()
protected void closeSocketAndStreams()
protected void assertNotOpen() throws IllegalStateException
IllegalStateException
if I am connected.
IllegalStateException
- if connectedprotected void assertOpen() throws IllegalStateException
IllegalStateException
if I am not connected.
IllegalStateException
- if not connected
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |