This interface defines the necessary methods
and constants to establish a secure network connection.
The URI format with scheme https when passed to
Connector.open will return a
HttpsConnection .
RFC 2818
defines the scheme.
A secure connection MUST be implemented by one or more
of the following specifications:
HTTPS is the secure version of HTTP (IETF RFC2616),
a request-response protocol in which the parameters of the request must
be set before the request is sent.
In addition to the normal IOExceptions that may occur during
invocation of the various methods that cause a transition to the Connected
state, CertificateException
(a subtype of IOException ) may be thrown to indicate various
failures related to establishing the secure link. The secure link
is necessary in the Connected state so the headers
can be sent securely. The secure link may be established as early as the
invocation of Connector.open() and related
methods for opening input and output streams and failure related to
certificate exceptions may be reported.
Example
Open a HTTPS connection, set its parameters, then read the HTTP
response.
Connector.open is used to open the URL
and an HttpsConnection is returned.
The HTTP
headers are read and processed. If the length is available, it is used
to read the data in bulk. From the
HttpsConnection the InputStream is
opened. It is used to read every character until end of file (-1). If
an exception is thrown the connection and stream are closed.
void getViaHttpsConnection(String url)
throws CertificateException, IOException {
HttpsConnection c = null;
InputStream is = null;
try {
c = (HttpsConnection)Connector.open(url);
// Getting the InputStream ensures that the connection
// is opened (if it was not already handled by
// Connector.open()) and the SSL handshake is exchanged,
// and the HTTP response headers are read.
// These are stored until requested.
is = c.openDataInputStream();
if c.getResponseCode() == HttpConnection.HTTP_OK) {
// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
byte[] data = new byte[len];
int actual = is.readFully(data);
...
} else {
int ch;
while ((ch = is.read()) != -1) {
...
}
}
} else {
...
}
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
}
See Also: CertificateException |