Class HttpsURLConnection
- Direct Known Subclasses:
HttpsURLConnectionImpl
public abstract class HttpsURLConnection extends HttpURLConnection
HttpURLConnection for HTTPS (RFC 2818). A
connected HttpsURLConnection allows access to the
negotiated cipher suite, the server certificate chain, and the
client certificate chain if any.
Providing an application specific X509TrustManager
If an application wants to trust Certificate Authority (CA) certificates that are not part of the system, it should specify its ownX509TrustManager via a SSLSocketFactory set on
the HttpsURLConnection. The X509TrustManager can be
created based on a KeyStore using a
TrustManagerFactory to supply trusted CA certificates. Note that
self-signed certificates are effectively their own CA and can be
trusted by including them in a KeyStore.
For example, to trust a set of certificates specified by a KeyStore:
KeyStore keyStore = ...;
String algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
tmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
URL url = new URL("https://www.example.com/");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
InputStream in = urlConnection.getInputStream();
It is possible to implement X509TrustManager directly
instead of using one created by a
TrustManagerFactory. While this is straightforward in the insecure
case of allowing all certificate chains to pass verification,
writing a proper implementation will usually want to take advantage
of CertPathValidator. In general, it might be better to write a
custom KeyStore implementation to pass to the
TrustManagerFactory than to try and write a custom
X509TrustManager.
Providing an application specific X509KeyManager
A customX509KeyManager can be used to supply a client
certificate and its associated private key to authenticate a
connection to the server. The X509KeyManager can be created
based on a KeyStore using a KeyManagerFactory.
For example, to supply client certificates from a KeyStore:
KeyStore keyStore = ...;
String algorithm = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(kmf.getKeyManagers(), null, null);
URL url = new URL("https://www.example.com/");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
InputStream in = urlConnection.getInputStream();
A X509KeyManager can also be implemented directly. This
can allow an application to return a certificate and private key
from a non-KeyStore source or to specify its own logic for
selecting a specific credential to use when many may be present in
a single KeyStore.
TLS Intolerance Support
This class attempts to create secure connections using common TLS extensions and SSL deflate compression. Should that fail, the connection will be retried with SSLv3 only.-
Field Summary
Fields Modifier and Type Field Description protected HostnameVerifierhostnameVerifierThe host name verifier used by this connection.Fields inherited from class java.net.HttpURLConnection
chunkLength, fixedContentLength, fixedContentLengthLong, HTTP_ACCEPTED, HTTP_BAD_GATEWAY, HTTP_BAD_METHOD, HTTP_BAD_REQUEST, HTTP_CLIENT_TIMEOUT, HTTP_CONFLICT, HTTP_CREATED, HTTP_ENTITY_TOO_LARGE, HTTP_FORBIDDEN, HTTP_GATEWAY_TIMEOUT, HTTP_GONE, HTTP_INTERNAL_ERROR, HTTP_LENGTH_REQUIRED, HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_MULT_CHOICE, HTTP_NO_CONTENT, HTTP_NOT_ACCEPTABLE, HTTP_NOT_AUTHORITATIVE, HTTP_NOT_FOUND, HTTP_NOT_IMPLEMENTED, HTTP_NOT_MODIFIED, HTTP_OK, HTTP_PARTIAL, HTTP_PAYMENT_REQUIRED, HTTP_PRECON_FAILED, HTTP_PROXY_AUTH, HTTP_REQ_TOO_LONG, HTTP_RESET, HTTP_SEE_OTHER, HTTP_SERVER_ERROR, HTTP_UNAUTHORIZED, HTTP_UNAVAILABLE, HTTP_UNSUPPORTED_TYPE, HTTP_USE_PROXY, HTTP_VERSION, instanceFollowRedirects, method, responseCode, responseMessageFields inherited from class java.net.URLConnection
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches -
Constructor Summary
Constructors Modifier Constructor Description protectedHttpsURLConnection(URL url)Creates a newHttpsURLConnectionwith the specifiedURL. -
Method Summary
Modifier and Type Method Description abstract StringgetCipherSuite()Returns the name of the cipher suite negotiated during the SSL handshake.static HostnameVerifiergetDefaultHostnameVerifier()Returns the default hostname verifier.static SSLSocketFactorygetDefaultSSLSocketFactory()Returns the default SSL socket factory for new instances.HostnameVerifiergetHostnameVerifier()Returns the hostname verifier used by this instance.abstract Certificate[]getLocalCertificates()Returns the list of local certificates used during the handshake.PrincipalgetLocalPrincipal()Returns thePrincipalused to identify the local host during the handshake.PrincipalgetPeerPrincipal()Returns thePrincipalidentifying the peer.abstract Certificate[]getServerCertificates()Return the list of certificates identifying the peer during the handshake.SSLSocketFactorygetSSLSocketFactory()Returns the SSL socket factory used by this instance.static voidsetDefaultHostnameVerifier(HostnameVerifier v)Sets the default hostname verifier to be used by new instances.static voidsetDefaultSSLSocketFactory(SSLSocketFactory sf)Sets the default SSL socket factory to be used by new instances.voidsetHostnameVerifier(HostnameVerifier v)Sets the hostname verifier for this instance.voidsetSSLSocketFactory(SSLSocketFactory sf)Sets the SSL socket factory for this instance.Methods inherited from class java.net.HttpURLConnection
disconnect, getContentEncoding, getErrorStream, getFollowRedirects, getHeaderFieldDate, getInstanceFollowRedirects, getPermission, getRequestMethod, getResponseCode, getResponseMessage, setChunkedStreamingMode, setFixedLengthStreamingMode, setFixedLengthStreamingMode, setFollowRedirects, setInstanceFollowRedirects, setRequestMethod, usingProxyMethods inherited from class java.net.URLConnection
addRequestProperty, connect, getAllowUserInteraction, getConnectTimeout, getContent, getContent, getContentLength, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderField, getHeaderField, getHeaderFieldInt, getHeaderFieldKey, getHeaderFields, getIfModifiedSince, getInputStream, getLastModified, getOutputStream, getReadTimeout, getRequestProperties, getRequestProperty, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setConnectTimeout, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setIfModifiedSince, setReadTimeout, setRequestProperty, setUseCaches, toString
-
Field Details
-
hostnameVerifier
The host name verifier used by this connection. It is initialized from the default hostname verifiersetDefaultHostnameVerifier(HostnameVerifier)orgetDefaultHostnameVerifier().
-
-
Constructor Details
-
HttpsURLConnection
Creates a newHttpsURLConnectionwith the specifiedURL.- Parameters:
url- theURLto connect to.
-
-
Method Details
-
setDefaultHostnameVerifier
Sets the default hostname verifier to be used by new instances.- Parameters:
v- the new default hostname verifier- Throws:
IllegalArgumentException- if the specified verifier isnull.
-
getDefaultHostnameVerifier
Returns the default hostname verifier.- Returns:
- the default hostname verifier.
-
setDefaultSSLSocketFactory
Sets the default SSL socket factory to be used by new instances.- Parameters:
sf- the new default SSL socket factory.- Throws:
IllegalArgumentException- if the specified socket factory isnull.
-
getDefaultSSLSocketFactory
Returns the default SSL socket factory for new instances.- Returns:
- the default SSL socket factory for new instances.
-
getCipherSuite
Returns the name of the cipher suite negotiated during the SSL handshake.- Returns:
- the name of the cipher suite negotiated during the SSL handshake.
- Throws:
IllegalStateException- if no connection has been established yet.
-
getLocalCertificates
Returns the list of local certificates used during the handshake. These certificates were sent to the peer.- Returns:
- Returns the list of certificates used during the handshake with
the local identity certificate followed by CAs, or
nullif no certificates were used during the handshake. - Throws:
IllegalStateException- if no connection has been established yet.
-
getServerCertificates
Return the list of certificates identifying the peer during the handshake.- Returns:
- the list of certificates identifying the peer with the peer's identity certificate followed by CAs.
- Throws:
SSLPeerUnverifiedException- if the identity of the peer has not been verified..IllegalStateException- if no connection has been established yet.
-
getPeerPrincipal
Returns thePrincipalidentifying the peer.- Returns:
- the
Principalidentifying the peer. - Throws:
SSLPeerUnverifiedException- if the identity of the peer has not been verified.IllegalStateException- if no connection has been established yet.
-
getLocalPrincipal
Returns thePrincipalused to identify the local host during the handshake.- Returns:
- the
Principalused to identify the local host during the handshake, ornullif none was used. - Throws:
IllegalStateException- if no connection has been established yet.
-
setHostnameVerifier
Sets the hostname verifier for this instance.- Parameters:
v- the hostname verifier for this instance.- Throws:
IllegalArgumentException- if the specified verifier isnull.
-
getHostnameVerifier
Returns the hostname verifier used by this instance.- Returns:
- the hostname verifier used by this instance.
-
setSSLSocketFactory
Sets the SSL socket factory for this instance.- Parameters:
sf- the SSL socket factory to be used by this instance.- Throws:
IllegalArgumentException- if the specified socket factory isnull.
-
getSSLSocketFactory
Returns the SSL socket factory used by this instance.- Returns:
- the SSL socket factory used by this instance.
-