public class HttpConnection extends Object
The underlying HttpsURLConnection is transparently managed by Java. To reuse connections, for each time connect() is called, the input streams (input stream or error stream, if input stream is not accessible) must be completely read. Otherwise, the data remains in the stream and the connection will not be reusable.
| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
body
The body.
|
protected HttpsURLConnection |
connection
The underlying HTTPS connection.
|
| Modifier | Constructor and Description |
|---|---|
protected |
HttpConnection() |
|
HttpConnection(URL url,
HttpMethod method)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
connect()
Sends the request to the URL given in the constructor.
|
Map<String,List<String>> |
getResponseHeaders()
Returns the response headers as a Map, where the key is the header field
name and the values are the values associated with the header field
name.
|
int |
getResponseStatus()
Returns the response status code.
|
byte[] |
readError()
Reads from the error stream and returns the error reason.
|
byte[] |
readInput()
Reads from the input stream (response stream) and returns the response.
|
protected static byte[] |
readInputStream(InputStream stream)
Reads the input stream until the stream is empty.
|
void |
setReadTimeoutMillis(int timeout)
Sets the read timeout in milliseconds.
|
void |
setRequestHeader(String field,
String value)
Sets the request header field to the given value.
|
void |
setRequestMethod(HttpMethod method)
Sets the request method (i.e.
|
void |
writeOutput(byte[] body)
Saves the body to be sent with the request.
|
protected final HttpsURLConnection connection
protected byte[] body
public HttpConnection(URL url, HttpMethod method) throws IOException
url - The URL for the HTTPS connection.method - The HTTPS method (i.e. GET).IOException - This exception is thrown if the connection was unable to be opened.protected HttpConnection()
public void connect()
throws IOException
IOException - This exception thrown if the connection could not be established,
or the server responded with a bad status code.public void setRequestMethod(HttpMethod method)
method - The request method.IllegalArgumentException - This exception thrown if the request currently
has a non-empty body and the new method is not a POST or a PUT. This is because Java's
HttpsURLConnection silently converts the HTTPS method to POST or PUT if a
body is written to the request.public void setRequestHeader(String field, String value)
field - The header field name.value - The header field value.public void setReadTimeoutMillis(int timeout)
timeout - The read timeout.public void writeOutput(byte[] body)
body - The request body.IllegalArgumentException - if the request does not currently use
method POST or PUT and the body is non-empty. This is because Java's
HttpsURLConnection silently converts the HTTPS method to POST or PUT if a
body is written to the request.public byte[] readInput()
throws IOException
IOException - This exception thrown if the input stream could not be
accessed, for example if the server could not be reached.public byte[] readError()
throws IOException
IOException - This exception thrown if the input stream could not be
accessed, for example if the server could not be reached.public int getResponseStatus()
throws IOException
IOException - This exception thrown if no response was received.public Map<String,List<String>> getResponseHeaders() throws IOException
IOException - This exception thrown if no response was received.protected static byte[] readInputStream(InputStream stream) throws IOException
stream - The input stream.IOException - This exception thrown if the input stream could not be read from.Copyright © 2019. All rights reserved.