HttpsConnection Class

  • java.lang.Object
    • com.microsoft.azure.sdk.iot.device.transport.https.HttpsConnection

public class HttpsConnection

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.

Constructor Summary

Constructor Description
HttpsConnection()
HttpsConnection(URL url, HttpsMethod method)

Constructor. Opens a connection to the given URL. Can be HTTPS or HTTP

HttpsConnection(URL url, HttpsMethod method, final ProxySettings proxySettings)

Constructor. Opens a connection to the given URL. Can be HTTPS or HTTP

HttpsConnection(URL url, HttpsMethod method, final ProxySettings proxySettings, boolean isHttps)

Constructor. Opens a connection to the given URL. Can be HTTPS or HTTP

Method Summary

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.

void setConnectTimeout(int timeout)

Sets the connect timeout in milliseconds.

void setReadTimeout(int timeout)

Sets the read timeout in milliseconds. The read timeout is the number of milliseconds after the server receives a request and before the server sends data back.

void setRequestHeader(String field, String value)

Sets the request header field to the given value.

void setRequestMethod(HttpsMethod method)

Sets the request method (i.e. POST).

void writeOutput(byte[] body)

Saves the body to be sent with the request.

Constructor Details

HttpsConnection

protected HttpsConnection()

HttpsConnection

public HttpsConnection(URL url, HttpsMethod method)

Constructor. Opens a connection to the given URL. Can be HTTPS or HTTP

Parameters:

url - the URL for the HTTP/HTTPS connection.
method - the HTTP method (i.e. GET).

Throws:

TransportException - if the connection could not be opened.

HttpsConnection

public HttpsConnection(URL url, HttpsMethod method, final ProxySettings proxySettings)

Constructor. Opens a connection to the given URL. Can be HTTPS or HTTP

Parameters:

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 used

Throws:

TransportException - if the connection could not be opened.

HttpsConnection

public HttpsConnection(URL url, HttpsMethod method, final ProxySettings proxySettings, boolean isHttps)

Constructor. Opens a connection to the given URL. Can be HTTPS or HTTP

Parameters:

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 used
isHttps - if true, then this request is an https request as opposed to an http request

Throws:

TransportException - if the connection could not be opened.

Method Details

connect

public void connect()

Sends the request to the URL given in the constructor.

Throws:

TransportException - if the connection could not be established, or the server responded with a bad status code.

getResponseHeaders

public Map> 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.

Returns:

the response headers.

getResponseStatus

public int getResponseStatus()

Returns the response status code.

Returns:

the response status code.

Throws:

TransportException - if no response was received.

readError

public byte [] readError()

Reads from the error stream and returns the error reason.

Returns:

the error reason.

Throws:

TransportException - if the input stream could not be accessed, for example if the server could not be reached.

readInput

public byte [] readInput()

Reads from the input stream (response stream) and returns the response.

Returns:

the response body.

Throws:

TransportException - if the input stream could not be accessed, for example if the server could not be reached.

setConnectTimeout

public void setConnectTimeout(int timeout)

Sets the connect timeout in milliseconds.

Parameters:

timeout - the connect timeout in milliseconds.

setReadTimeout

public void setReadTimeout(int timeout)

Sets the read timeout in milliseconds. The read timeout is the number of milliseconds after the server receives a request and before the server sends data back.

Parameters:

timeout - the read timeout.

setRequestHeader

public void setRequestHeader(String field, String value)

Sets the request header field to the given value.

Parameters:

field - the header field name.
value - the header field value.

setRequestMethod

public void setRequestMethod(HttpsMethod method)

Sets the request method (i.e. POST).

Parameters:

method - the request method.

Throws:

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.

writeOutput

public void writeOutput(byte[] body)

Saves the body to be sent with the request.

Parameters:

body - the request body.

Applies to