public class SimpleHTTPClient
extends java.lang.Object
This client allows synchronous HTTP communication.
HTTPClient client = HTTPClient.create("http://127.0.0.1:8080/test");
// Send a simple GET request for /test
OutputStream out = client.exchange();
// Get the response
HTTP header HTTPResponseHeader response = client.getResponseHeader();
// Get the response HTTP body
InputStream in = client.getResponseBody();
// Send a simple GET request for /hello
OutputStream out = client.exchange("/hello");
// Get the response HTTP header
HTTPResponseHeader response = client.getResponseHeader();
// Get the response HTTP body
InputStream in = client.getResponseBody();
// Close the communication
client.close();
| Constructor and Description |
|---|
SimpleHTTPClient(HTTPClientDestination dest)
Creates a HTTP client to communicate with given host.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the HTTP connection between client and server.
|
java.io.OutputStream |
exchange()
Sends a simple HTTP GET request to the host, defined by the
create method. |
java.io.OutputStream |
exchange(HTTPRequestHeader header,
boolean sendHeader)
Sends a HTTP request with given HTTP header to the host, defined by the
create
method. |
java.io.OutputStream |
exchange(java.lang.String request)
Sends a simple HTTP GET request with given request path to the host, defined by the
create method. |
java.io.OutputStream |
exchange(java.lang.String method,
java.lang.String request)
Sends a HTTP request with a given HTTP method and request path to the host, defined by the
create method. |
void |
explicitConnect()
Opens a TCP connection to the defined host.
|
TCPConnection |
getConnection() |
CredentialInfo |
getCredentialInfo() |
java.lang.String |
getPresetRequest()
Returns the preset HTTP path for the request.
|
HTTPRequestHeader |
getRequestHeader()
Returns the HTTP request header.
|
java.io.InputStream |
getResponseBody()
Returns an
InputStream which allows to read the HTTP response body. |
HTTPInputStream |
getResponseBody(Sync syn) |
HTTPResponseHeader |
getResponseHeader()
Returns the HTTP response header.
|
boolean |
isKeepAlive()
Returns
true if the HTTP connection must stay persistent, false
otherwise. |
void |
resetConnection()
Resets the connection.
|
void |
sendHeader() |
public SimpleHTTPClient(HTTPClientDestination dest)
The given request MUST be a HTTP address. The path behind host and port will be used in the
exchange() method as request. The method exchange(String) and exchange(String, String) expect the request on their own.
dest - public java.lang.String getPresetRequest()
This request is used as default for the exchange() method.
public void explicitConnect()
throws java.io.IOException
Usually the exchange methods will open the TCP connection on their own, but
sometimes it may be necessary to have previously opened the connection.
java.io.IOException - Throws exception if connection fails.public void resetConnection()
java.io.IOExceptionpublic java.io.OutputStream exchange()
throws java.io.IOException
create method.OutputStream which can be used to send HTTP body as part of the request.java.io.IOException - Throws exception if sending the HTTP header fails.public java.io.OutputStream exchange(java.lang.String request)
throws java.io.IOException
create method.request - the HTTP request path.OutputStream which can be used to send HTTP body as part of the request.java.io.IOException - Throws exception if sending the HTTP header fails.public java.io.OutputStream exchange(java.lang.String method,
java.lang.String request)
throws java.io.IOException
create method.
The HTTP method can be GET or POST.
method - HTTP method. e.g. GET or POST.request - The HTTP request path.OutputStream which can be used to send HTTP body as part of the request.java.io.IOException - Throws exception if sending the HTTP header fails.public java.io.OutputStream exchange(HTTPRequestHeader header, boolean sendHeader) throws java.io.IOException
create
method.header - The HTTP header.OutputStream which can be used to send HTTP body as part of the request.java.io.IOException - Throws exception sending HTTP header fails.public void sendHeader()
throws java.io.IOException
java.io.IOExceptionpublic TCPConnection getConnection()
public HTTPRequestHeader getRequestHeader()
public HTTPResponseHeader getResponseHeader() throws java.io.IOException
java.io.IOException - Throws exception receiving the header fails.public java.io.InputStream getResponseBody()
InputStream which allows to read the HTTP response body.InputStream which allows to read the HTTP response body.public HTTPInputStream getResponseBody(Sync syn)
public boolean isKeepAlive()
true if the HTTP connection must stay persistent, false
otherwise.true if the HTTP connection must stay persistent, false otherwise.public void close()
throws java.io.IOException
java.io.IOException - Throws exceptionpublic CredentialInfo getCredentialInfo()