Class HttpsURLConnection
- java.lang.Object
-
- java.net.URLConnection
-
- java.net.HttpURLConnection
-
- javax.net.ssl.HttpsURLConnection
-
- Direct Known Subclasses:
HttpsURLConnectionImpl
public abstract class HttpsURLConnection extends HttpURLConnection
AnHttpURLConnectionfor HTTPS (RFC 2818). A connectedHttpsURLConnectionallows 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 ownX509TrustManagervia aSSLSocketFactoryset on theHttpsURLConnection. TheX509TrustManagercan be created based on aKeyStoreusing aTrustManagerFactoryto supply trusted CA certificates. Note that self-signed certificates are effectively their own CA and can be trusted by including them in aKeyStore.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
X509TrustManagerdirectly instead of using one created by aTrustManagerFactory. 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 ofCertPathValidator. In general, it might be better to write a customKeyStoreimplementation to pass to theTrustManagerFactorythan to try and write a customX509TrustManager.Providing an application specific X509KeyManager
A customX509KeyManagercan be used to supply a client certificate and its associated private key to authenticate a connection to the server. TheX509KeyManagercan be created based on aKeyStoreusing aKeyManagerFactory.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
X509KeyManagercan also be implemented directly. This can allow an application to return a certificate and private key from a non-KeyStoresource or to specify its own logic for selecting a specific credential to use when many may be present in a singleKeyStore.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, responseMessage
-
Fields 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
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 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, usingProxy
-
Methods 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 Detail
-
hostnameVerifier
protected HostnameVerifier hostnameVerifier
The host name verifier used by this connection. It is initialized from the default hostname verifiersetDefaultHostnameVerifier(HostnameVerifier)orgetDefaultHostnameVerifier().
-
-
Constructor Detail
-
HttpsURLConnection
protected HttpsURLConnection(URL url)
Creates a newHttpsURLConnectionwith the specifiedURL.- Parameters:
url- theURLto connect to.
-
-
Method Detail
-
setDefaultHostnameVerifier
public static void setDefaultHostnameVerifier(HostnameVerifier v)
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
public static HostnameVerifier getDefaultHostnameVerifier()
Returns the default hostname verifier.- Returns:
- the default hostname verifier.
-
setDefaultSSLSocketFactory
public static void setDefaultSSLSocketFactory(SSLSocketFactory sf)
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
public static SSLSocketFactory getDefaultSSLSocketFactory()
Returns the default SSL socket factory for new instances.- Returns:
- the default SSL socket factory for new instances.
-
getCipherSuite
public abstract String 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
public abstract Certificate[] 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
public abstract Certificate[] getServerCertificates() throws SSLPeerUnverifiedException
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
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException
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
public Principal 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
public void setHostnameVerifier(HostnameVerifier v)
Sets the hostname verifier for this instance.- Parameters:
v- the hostname verifier for this instance.- Throws:
IllegalArgumentException- if the specified verifier isnull.
-
getHostnameVerifier
public HostnameVerifier getHostnameVerifier()
Returns the hostname verifier used by this instance.- Returns:
- the hostname verifier used by this instance.
-
setSSLSocketFactory
public void setSSLSocketFactory(SSLSocketFactory sf)
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
public SSLSocketFactory getSSLSocketFactory()
Returns the SSL socket factory used by this instance.- Returns:
- the SSL socket factory used by this instance.
-
-