public class HttpConnection
extends java.lang.Object
A wrapper for HttpURLConnection
s.
Provides some convenience methods for making requests and sending/receiving data as streams, strings, or byte arrays.
Typical usage:
HttpConnection hc = new HttpConnection("POST", "application/json", new URL("http://somewhere")); hc.requestProperties.put("x-some-header", "some-value"); hc.setRequestBody("{\"hello\": \"world\"}); String result = hc.execute().responseAsString(); // get the underlying HttpURLConnection if you need to do something a bit more advanced: int response = hc.getConnection().getResponseCode(); hc.disconnect();
Important: this class is not thread-safe and HttpConnection
s should not be
shared across threads.
HttpURLConnection
Modifier and Type | Class and Description |
---|---|
static interface |
HttpConnection.InputStreamGenerator |
Modifier and Type | Field and Description |
---|---|
java.util.List<HttpConnectionRequestInterceptor> |
requestInterceptors |
java.util.HashMap<java.lang.String,java.lang.String> |
requestProperties |
java.util.List<HttpConnectionResponseInterceptor> |
responseInterceptors |
java.net.URL |
url |
Constructor and Description |
---|
HttpConnection(java.lang.String requestMethod,
java.net.URL url,
java.lang.String contentType) |
Modifier and Type | Method and Description |
---|---|
void |
disconnect()
Disconnect the underlying HttpURLConnection.
|
HttpConnection |
execute()
Execute request without returning data from server.
|
java.net.HttpURLConnection |
getConnection()
Get the underlying HttpURLConnection object, allowing clients to set/get properties not
exposed here.
|
byte[] |
responseAsBytes()
Return response body data from server as a byte array.
|
java.io.InputStream |
responseAsInputStream()
Return response body data from server as an InputStream.
|
java.lang.String |
responseAsString()
Return response body data from server as a String.
|
HttpConnection |
setNumberOfRetries(int numberOfRetries)
Sets the number of times this request can be retried.
|
HttpConnection |
setRequestBody(byte[] input)
Set the byte array of request body data to be sent to the server.
|
HttpConnection |
setRequestBody(HttpConnection.InputStreamGenerator input)
Set the InputStream of request body data to be sent to the server.
|
HttpConnection |
setRequestBody(HttpConnection.InputStreamGenerator input,
long inputLength)
Set the InputStream of request body data, of known length, to be sent to the server.
|
HttpConnection |
setRequestBody(java.lang.String input)
Set the String of request body data to be sent to the server.
|
public final java.net.URL url
public final java.util.HashMap<java.lang.String,java.lang.String> requestProperties
public final java.util.List<HttpConnectionRequestInterceptor> requestInterceptors
public final java.util.List<HttpConnectionResponseInterceptor> responseInterceptors
public HttpConnection(java.lang.String requestMethod, java.net.URL url, java.lang.String contentType)
public HttpConnection setNumberOfRetries(int numberOfRetries)
execute()
numberOfRetries
- the number of times this request can be retried.HttpConnection
for method chainingpublic HttpConnection setRequestBody(java.lang.String input)
input
- String of request body data to be sent to the server.
The input is assumed to be UTF-8 encoded.HttpConnection
for method chainingpublic HttpConnection setRequestBody(byte[] input)
input
- byte array of request body data to be sent to the serverHttpConnection
for method chainingpublic HttpConnection setRequestBody(HttpConnection.InputStreamGenerator input)
input
- InputStream of request body data to be sent to the serverHttpConnection
for method chainingpublic HttpConnection setRequestBody(HttpConnection.InputStreamGenerator input, long inputLength)
input
- InputStream of request body data to be sent to the serverinputLength
- Length of request body data to be sent to the server, in bytesHttpConnection
for method chainingpublic HttpConnection execute() throws java.io.IOException
Execute request without returning data from server.
Call responseAsString
, responseAsBytes
, or responseAsInputStream
after execute
if the response body is required.
HttpConnection
which can be used to obtain the response bodyjava.io.IOException
- if there was a problem writing data to the serverpublic java.lang.String responseAsString() throws java.io.IOException
Return response body data from server as a String.
Important: you must call execute()
before calling this method.
java.io.IOException
- if there was a problem reading data from the serverpublic byte[] responseAsBytes() throws java.io.IOException
Return response body data from server as a byte array.
Important: you must call execute()
before calling this method.
java.io.IOException
- if there was a problem reading data from the serverpublic java.io.InputStream responseAsInputStream() throws java.io.IOException
Return response body data from server as an InputStream.
Important: you must call execute()
before calling this method.
java.io.IOException
- if there was a problem reading data from the serverpublic java.net.HttpURLConnection getConnection()
HttpURLConnection
objectpublic void disconnect()
getConnection.disconnect()