public class HttpConnection
extends java.lang.Object
A wrapper for HttpURLConnections.
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 HttpConnections should not be
shared across threads.
HttpURLConnection| Modifier and Type | Class and Description |
|---|---|
static interface |
HttpConnection.HttpUrlConnectionFactory
Factory used by HttpConnection to produce HttpUrlConnections.
|
static interface |
HttpConnection.InputStreamGenerator
An InputStreamGenerator has a single method getInputStream.
|
| Modifier and Type | Field and Description |
|---|---|
HttpConnection.HttpUrlConnectionFactory |
connectionFactory
A connectionFactory for opening the URLs, can be set, but configured with a default
|
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 an InputStreamGenerator for an InputStream of request body data to be sent to the server.
|
HttpConnection |
setRequestBody(HttpConnection.InputStreamGenerator input,
long inputLength)
Set an InputStreamGenerator for an InputStream, of known length, of request body data to
be sent to the server.
|
HttpConnection |
setRequestBody(java.io.InputStream input)
Deprecated.
|
HttpConnection |
setRequestBody(java.io.InputStream input,
long inputLength)
Deprecated.
|
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.HttpUrlConnectionFactory connectionFactory
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 serverHttpConnection 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(java.io.InputStream input)
setRequestBody(InputStreamGenerator)input - InputStream of request body data to be sent to the serverHttpConnection for method chainingpublic HttpConnection setRequestBody(java.io.InputStream input, long inputLength)
setRequestBody(InputStreamGenerator, long)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 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 - InputStreamGenerator that returns an 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.
Note if the URL contains user information it will be encoded in a BasicAuth header.
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()