public class HttpsConnection
extends java.lang.Object
A wrapper for the Java SE class HttpsURLConnection. Used to avoid
compatibility issues when testing with the mocking framework JMockit, as well
as to avoid some undocumented side effects when using HttpsURLConnection.
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 | Constructor and Description |
|---|---|
protected |
HttpsConnection() |
|
HttpsConnection(java.net.URL url,
HttpsMethod method)
Constructor.
|
|
HttpsConnection(java.net.URL url,
HttpsMethod method,
ProxySettings proxySettings)
Constructor.
|
|
HttpsConnection(java.net.URL url,
HttpsMethod method,
ProxySettings proxySettings,
boolean isHttps)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
connect()
Sends the request to the URL given in the constructor.
|
java.util.Map<java.lang.String,java.util.List<java.lang.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.
|
void |
setConnectTimeout(int timeout)
Sets the connect timeout in milliseconds.
|
void |
setReadTimeout(int timeout)
Sets the read timeout in milliseconds.
|
void |
setRequestHeader(java.lang.String field,
java.lang.String value)
Sets the request header field to the given value.
|
void |
setRequestMethod(HttpsMethod method)
Sets the request method (i.e.
|
void |
writeOutput(byte[] body)
Saves the body to be sent with the request.
|
public HttpsConnection(java.net.URL url,
HttpsMethod method)
throws TransportException
url - the URL for the HTTP/HTTPS connection.method - the HTTP method (i.e. GET).TransportException - if the connection could not be opened.public HttpsConnection(java.net.URL url,
HttpsMethod method,
ProxySettings proxySettings)
throws TransportException
url - the URL for the HTTP/HTTPS connection.method - the HTTP method (i.e. GET).proxySettings - The proxy settings to use when connecting. If null, then no proxy will be usedTransportException - if the connection could not be opened.public HttpsConnection(java.net.URL url,
HttpsMethod method,
ProxySettings proxySettings,
boolean isHttps)
throws TransportException
url - the URL for the HTTP/HTTPS connection.method - the HTTP method (i.e. GET).proxySettings - The proxy settings to use when connecting. If null, then no proxy will be usedisHttps - if true, then this request is an https request as opposed to an http requestTransportException - if the connection could not be opened.protected HttpsConnection()
public void connect()
throws TransportException
TransportException - if the connection could not be established, or the
server responded with a bad status code.public void setRequestMethod(HttpsMethod method) throws TransportException
method - the request method.TransportException - 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(java.lang.String field,
java.lang.String value)
field - the header field name.value - the header field value.public void setReadTimeout(int timeout)
timeout - the read timeout.public void setConnectTimeout(int timeout)
timeout - the connect timeout in milliseconds.public void writeOutput(byte[] body)
body - the request body.public byte[] readInput()
throws TransportException
TransportException - if the input stream could not be accessed, for
example if the server could not be reached.public byte[] readError()
throws TransportException
TransportException - if the input stream could not be accessed, for
example if the server could not be reached.public int getResponseStatus()
throws TransportException
TransportException - if no response was received.public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getResponseHeaders()
Map, where the key is the
header field name and the values are the values associated with the
header field name.Copyright © 2023. All rights reserved.