public class EasyHttpClient extends Object
The underlying connection is kept active until close() is called.
After every operation, the data sent by the HTTP server must be fully read,
otherwise, any following operation on the same connection may not execute
successfully. The character readers returned can be read or discarded
easily with the HttpUtil.read(java.io.InputStream) and HttpUtil.discard(java.io.BufferedReader) methods.
If the HTTP connection is to be used again, do not call
close on any returned character readers.
Example GET usage: (Retrieves the root document)
URL url = new URL("http://example.com");
EasyHttpClient client = new EasyHttpClient(url);
String document = HttpUtil.read(client.doGet());
client.setFile("/somedir/somefile.html");
String document2 = HttpUtil.read(client.doGet());
client.close();
Example POST usage: (Posts URL encoded data to the same CGI script)
URL url = new URL("http://example.com/post.cgi");
EasyHttpClientFactory factory = new EasyHttpClientFactory();
EasyHttpClient client = factory.makePostClient(url);
BufferedReader br;
java.util.Map map = new HashMap();
map.put("name", "Joe");
map.put("business", "Bar");
br = client.doPostUrlEncoded(HttpUtil.urlEncode(map));
EasyHttpClient.discard(br);
map.put("name", "Alice");
map.put("business", "Foo");
br = client.doPostUrlEncoded(HttpUtil.urlEncode(map));
EasyHttpClient.discard(br);
client.close();
Example DELETE method usage: (Deletes two remote files)
URL url = new URL("http://example.com/somefile");
EasyHttpClientFactory factory = new EasyHttpClientFactory();
EasyHttpClient client = factory.makeClient(url, RequestLine.METHOD_DELETE);
client.doOperation();
client.setFile("/somefile2");
client.doOperation();
Example binary POST method usage: (Posts an image to a CGI script)
URL url = new URL("http://example.com/post.cgi");
EasyHttpClientFactory factory = new EasyHttpClientFactory();
EasyHttpClient client = factory.makePostClient();
InputStream fileIS = new FileInputStream("somefile.jpeg");
InputStream resultIS = client.doOperation(fileIS, -1, "image/jpeg");
Design notes: This class is designed as a wrapper,
allowing the underlying HttpClient behavior to be delegated
in another class. The goal is to provide a lot of functionality
that users will not have to re-implement deal with many of the common
HTTP use-cases. If something significant must be altered in what
HTTP-level functionality is needed, it should be implementable
by extending an existing HttpClient. The operations
on any HttpClient can then be simply extended with this wrapper.
EasyHttpClientFactory,
HttpClient| Constructor and Description |
|---|
EasyHttpClient(HttpClient client,
RequestLine requestLine,
MessageHeaders headers)
Constructs a new HTTP client with a specific wrapped
client, request line, and headers.
|
EasyHttpClient(HttpClient c,
URL url,
Method method)
Constructs a new HTTP client.
|
EasyHttpClient(URL url)
Constructs a new HTTP client.
|
EasyHttpClient(URL url,
Method method)
Constructs a new HTTP client.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the wrapped
HttpClient. |
BufferedReader |
doGet()
Performs a
GET operation,
returning a BufferedReader, which can be used to read the
response body. |
void |
doOperation()
Performs whatever operation was specified in the request line, as
passed into the constructor.
|
InputStream |
doOperation(InputStream is,
int len,
String contentType)
Performs whatever operation was specified in the request, as
passed into the constructor.
|
BufferedReader |
doPost(byte[] data,
int off,
int len,
String contentType)
Performs a
POST operation, returning a
BufferedReader for reading the response body. |
BufferedReader |
doPostUrlEncoded(byte[] urlEncodedData)
Performs a POST operation, returning a
BufferedReader
for reading the response body. |
MessageHeaders |
getHeaders()
Returns the message headers in use.
|
Response |
getLastResponse()
Returns the last HTTP response, including headers, resulting
from the last
doPost, doGet, or
doOperation call. |
static void |
main(String[] args)
Performs a command-line test.
|
void |
setCheckStatus(boolean checkStatus)
Sets if the status will automatically be checked for a 200-level
response or whether or not the status will be ignored.
|
void |
setFile(String fileName)
Allows a subsequent operation to be repeated with a different file on
the same connection.
|
void |
setMethod(Method method)
Allows a subsequent operation to be repeated with a different method on
the same connection.
|
String |
toString()
Returns debug information.
|
public EasyHttpClient(HttpClient client, RequestLine requestLine, MessageHeaders headers)
public EasyHttpClient(HttpClient c, URL url, Method method)
public EasyHttpClient(URL url)
public void setFile(String fileName)
public void setMethod(Method method)
public MessageHeaders getHeaders()
public void setCheckStatus(boolean checkStatus)
public Response getLastResponse()
doPost, doGet, or
doOperation call.
Returns null if no response information exists.public BufferedReader doGet() throws IOException
GET operation,
returning a BufferedReader, which can be used to read the
response body.HttpException - if the input stream could
not be created, or the status was not allowedIOExceptiongetLastResponse(),
setCheckStatus(boolean),
HttpUtil.read(java.io.InputStream),
HttpUtil.discard(java.io.BufferedReader)public BufferedReader doPost(byte[] data, int off, int len, String contentType) throws IOException
POST operation, returning a
BufferedReader for reading the response body. The data
type to be transferred may be indicated.data - source data arrayoff - zero-based offset in sourcelen - length of array to sendcontentType - content type to indicate, optionally
null to indicate no content typeIOExceptiongetLastResponse(),
setCheckStatus(boolean),
HttpUtil.read(java.io.InputStream),
HttpUtil.discard(java.io.BufferedReader)public BufferedReader doPostUrlEncoded(byte[] urlEncodedData) throws IOException
BufferedReader
for reading the response body. The content type header is set
to indicate the x-www-form-urlencoded type.urlEncodedData - data to send (ASCII format)IOExceptionHttpUtil.urlEncode(java.lang.String[], java.lang.String)public void doOperation()
throws IOException
doOperation method to have the data returned.HttpException - if (unexpectedly) HTTP was obtainedIOExceptionpublic InputStream doOperation(InputStream is, int len, String contentType) throws IOException
doPost and doGet methods. Utilizes the class GeneralDataPoster
to do the data posting.is - data stream to be copied and output over HTTP;
if null, no data is written; if the input stream
supports marking, the post operation may be retried,
if not any retry will throw an HttpExceptionlen - if len >= 0, sets the content-length header to this length;
if len < 0, sets the chunked-encoding headercontentType - if not null, specifies the data content type
in the requestHttpException - if the supplied input stream does not contain
enough data to be sentIllegalArgumentException - if the supplied input stream is null
and a non-zero length was indicatedIOExceptionHttpUtil.readFully(java.io.InputStream)public void close()
throws IOException
HttpClient.IOExceptionCopyright © 2012. All Rights Reserved.