public class HttpClientConnection extends Object
HttpClientConnection
provides a simple HTTP/1.1
compliant connection from a client to an HTTP server. This class does not
provide a full-blown HTTP connection, but it is rather optimized for what
ONC/RPC clients need in order to tunnel ONC remote procedure calls through
ordinary HTTP connections, thus penetrating firewalls.
A HttpClientConnection
is not that clever as you would
first expect. Rather you have to do some things for yourself, like
reconnecting dropped connections, and so on. While this sometimes result
in more labour on the caller's shoulders, this keeps resource wasting at
a minimum, and gives you full control over redirections and other mess --
you do want full control, right?.
For this reason, for instance, an HttpClientConnection
does not buffer the whole request before sending it to the server but
rather relies on the caller to supply the right content-length information.
This avoids unnecessary double buffering but instead creates the bas64
encoded content on-the-fly.
Of course, this client connection object does not touch the content, it just suplies the pipe to swallow the data.
Modifier and Type | Field and Description |
---|---|
static byte[] |
CRLF |
static int |
HTTP_DEFAULTPORT
Default port where HTTP servers listen for incomming requests.
|
Constructor and Description |
---|
HttpClientConnection(String hostname)
Constructs a new
HttpClientConnection . |
HttpClientConnection(String hostname,
int port)
Constructs a new
HttpClientConnection . |
Modifier and Type | Method and Description |
---|---|
int |
beginDecoding()
Begin receiving the content sent by the HTTP server.
|
void |
beginPostRequest(String path,
String mimeType,
int contentLength)
Starts a new HTTP "POST" request and sends all necessary HTTP header
fields.
|
void |
close()
Closes the connection to the HTTP server and frees up some resources.
|
void |
endDecoding()
This method silently discards any unread content, if the caller has
yet not read all content.
|
void |
endPostRequest()
Ends the HTTP "POST" request.
|
String |
getContentType()
Returns the content type (MIME type, charset, etc.).
|
boolean |
getKeepAlive() |
int |
getRemainingContentLength()
Returns amount of content still available (to be read).
|
int |
getResponseCode()
Retrieve the response code of the last remote procedure call via HTTP.
|
int |
getTimeout()
Retrieve the current timeout set for remote procedure calls.
|
protected void |
handleOption(String option,
String value)
Handle options sent by the HTTP server.
|
int |
readContentBytes(byte[] buffer,
int offset,
int length)
Read content sent by the HTTP server.
|
void |
setTimeout(int milliseconds)
Set the timout for sending or receiving information to/from the HTTP
server.
|
void |
writeContentBytes(byte[] bytes,
int offset,
int length)
Send (part) of the content to the HTTP server.
|
public static final int HTTP_DEFAULTPORT
public static final byte[] CRLF
public HttpClientConnection(String hostname)
HttpClientConnection
. The port used on
the HTTP server side is the default HTTP port, 80.hostname
- name (DNS name or IP dotted address) of host running
a HTTP server to which we want to connect to.public HttpClientConnection(String hostname, int port)
HttpClientConnection
.hostname
- name (DNS name or IP dotted address) of host running
a HTTP server to which we should connect to.port
- Port number where the HTTP server can be contacted.public void close()
close
it is still possible to open a new
connection to the HTTP server once again.public void beginPostRequest(String path, String mimeType, int contentLength) throws IOException
writeContentBytes(byte[], int, int)
method. Finally, to finish the request
he has to call the endPostRequest()
method.path
- Path to server object which handles the POST request.
For instance, this can be a CGI script (although it better should
not be one, except in the case of FAST CGI).mimeType
- MIME-classified type of content to be sent.contentLength
- Length of content to be sent. If negative, the
length is not known in advance. In this case, keeping the connection
alive is not possible, so callers should avoid this situation,
if possible.IOException
- if an I/O exception occurs when sending the
HTTP headers. In this case the connection is closed automatically
and the caller must not send any content. However, the caller
is free to give the request another try by calling
beginEncoding
again, thus opening a new HTTP connection.public void writeContentBytes(byte[] bytes, int offset, int length) throws IOException
bytes
- The data.offset
- Start offset in the data.length
- Number of bytes to write.RuntimeException
- if too much content was sent.IOException
- if an I/O error occurs.NullPointerException
- if bytes
is
null
.IndexOutOfBoundsException
- if offset
is negative,
or length
is negative, or offset + length
is
greater than the length of the array bytes
.public void endPostRequest() throws IOException
IOException
- The post data of the post request could not be flushed.protected void handleOption(String option, String value)
Currently the following options are handled by this class:
option
- Name of option sent by HTTP server.value
- Value of option.public int beginDecoding() throws IOException
IOException
- Reading the HTTP header failed.public String getContentType()
public int readContentBytes(byte[] buffer, int offset, int length) throws IOException
buffer
- Buffer to receive the content sent by the HTTP server.offset
- Start offset in the buffer.length
- Number of bytes to receive.ProtocolException
- if not enough content was available (the
caller attempted to read more data than available) or if we received
junk information violating the HTTP protocol specification.IOException
- if an I/O error occurs.NullPointerException
- if bytes
is
null
.IndexOutOfBoundsException
- if offset
is negative,
or length
is negative, or offset + length
is
greater than the length of the array bytes
.public int getRemainingContentLength()
readContentBytes(byte[], int, int)
.public void endDecoding() throws IOException
This method silently discards any unread content, if the caller has yet not read all content.
IOException
- if reading remaining bytes fails.public void setTimeout(int milliseconds)
milliseconds
- Timeout in milliseconds.public int getTimeout()
public int getResponseCode()
public boolean getKeepAlive()
Copyright © 2022. All rights reserved.