Top Description Inners Fields Constructors Methods
javax.net.ssl

public abstract Class HttpsURLConnection

extends HttpURLConnection
Class Inheritance
Known Direct Subclasses
sun.net.www.protocol.https.HttpsURLConnectionImpl
Imports
java.net.URL, .HttpURLConnection, java.security.Principal, java.security.cert.X509Certificate, java.util.Optional

HttpsURLConnection extends HttpURLConnection with support for https-specific features.

See http://www.w3.org/pub/WWW/Protocols/ and RFC 2818 for more details on the https specification.

This class uses HostnameVerifier and SSLSocketFactory. There are default implementations defined for both classes. However, the implementations can be replaced on a per-class (static) or per-instance basis. All new HttpsURLConnections instances will be assigned the "default" static values at instance creation, but they can be overridden by calling the appropriate per-instance set method(s) before connecting.

Since
1.4

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static HostnameVerifier
defaultHostnameVerifier

HostnameVerifier provides a callback mechanism so that implementers of this interface can supply a policy for handling the case where the host to connect to and the server name from the certificate mismatch.

private static SSLSocketFactory
protected HostnameVerifier
hostnameVerifier

The hostnameVerifier for this object.

private SSLSocketFactory
sslSocketFactory

The SSLSocketFactory inherited when an instance of this class is created.

Inherited from java.net.HttpURLConnection:
chunkLengthfixedContentLengthfixedContentLengthLongHTTP_ACCEPTEDHTTP_BAD_GATEWAYHTTP_BAD_METHODHTTP_BAD_REQUESTHTTP_CLIENT_TIMEOUTHTTP_CONFLICTHTTP_CREATEDHTTP_ENTITY_TOO_LARGEHTTP_FORBIDDENHTTP_GATEWAY_TIMEOUTHTTP_GONEHTTP_INTERNAL_ERRORHTTP_LENGTH_REQUIREDHTTP_MOVED_PERMHTTP_MOVED_TEMPHTTP_MULT_CHOICEHTTP_NO_CONTENTHTTP_NOT_ACCEPTABLEHTTP_NOT_AUTHORITATIVEHTTP_NOT_FOUNDHTTP_NOT_IMPLEMENTEDHTTP_NOT_MODIFIEDHTTP_OKHTTP_PARTIALHTTP_PAYMENT_REQUIREDHTTP_PRECON_FAILEDHTTP_PROXY_AUTHHTTP_REQ_TOO_LONGHTTP_RESETHTTP_SEE_OTHERHTTP_SERVER_ERRORHTTP_UNAUTHORIZEDHTTP_UNAVAILABLEHTTP_UNSUPPORTED_TYPEHTTP_USE_PROXYHTTP_VERSIONinstanceFollowRedirectsmethodresponseCoderesponseMessage

Constructor Summary

AccessConstructor and Description
protected
HttpsURLConnection(URL
the URL
url
)

Creates an HttpsURLConnection using the URL specified.

Method Summary

Modifier and TypeMethod and Description
public abstract String

Returns:

the cipher suite
getCipherSuite
()

Returns the cipher suite in use on this connection.

public static HostnameVerifier

Returns:

the default host name verifier
getDefaultHostnameVerifier
()

Gets the default HostnameVerifier that is inherited by new instances of this class.

public static SSLSocketFactory

Returns:

the default SSLSocketFactory
getDefaultSSLSocketFactory
()

Gets the default static SSLSocketFactory that is inherited by new instances of this class.

public HostnameVerifier

Returns:

the host name verifier
getHostnameVerifier
()

Gets the HostnameVerifier in place on this instance.

public abstract Certificate[]

Returns:

an ordered array of certificates, with the client's own certificate first followed by any certificate authorities. If no certificates were sent, then null is returned.
getLocalCertificates
()

Returns the certificate(s) that were sent to the server during handshaking.

public Principal

Returns:

the principal sent to the server. Returns an X500Principal of the end-entity certificate for X509-based cipher suites, and KerberosPrincipal for Kerberos cipher suites. If no principal was sent, then null is returned.
getLocalPrincipal
()

Returns the principal that was sent to the server during handshaking.

public Principal

Returns:

the server's principal. Returns an X500Principal of the end-entity certificate for X509-based cipher suites, and KerberosPrincipal for Kerberos cipher suites.
getPeerPrincipal
()

Returns the server's principal which was established as part of defining the session.

public abstract Certificate[]

Returns:

an ordered array of server certificates, with the peer's own certificate first followed by any certificate authorities.
getServerCertificates
()

Returns the server's certificate chain which was established as part of defining the session.

public Optional<SSLSession>

Returns:

an Optional containing the SSLSession in use on this connection.
getSSLSession
()

Returns an Optional containing the SSLSession in use on this connection.

public SSLSocketFactory

Returns:

the SSLSocketFactory
getSSLSocketFactory
()

Gets the SSL socket factory to be used when creating sockets for secure https URL connections.

public static void
setDefaultHostnameVerifier(HostnameVerifier
the default host name verifier
v
)

Sets the default HostnameVerifier inherited by a new instance of this class.

public static void
setDefaultSSLSocketFactory(SSLSocketFactory
the default SSL socket factory
sf
)

Sets the default SSLSocketFactory inherited by new instances of this class.

public void
setHostnameVerifier(HostnameVerifier
the host name verifier
v
)

Sets the HostnameVerifier for this instance.

public void
setSSLSocketFactory(SSLSocketFactory
the SSL socket factory
sf
)

Sets the SSLSocketFactory to be used when this instance creates sockets for secure https URL connections.

Inherited from java.net.HttpURLConnection:
disconnectgetErrorStreamgetFollowRedirectsgetHeaderFieldgetHeaderFieldDategetHeaderFieldKeygetInstanceFollowRedirectsgetPermissiongetRequestMethodgetResponseCodegetResponseMessagesetAuthenticatorsetChunkedStreamingModesetFixedLengthStreamingModesetFixedLengthStreamingModesetFollowRedirectssetInstanceFollowRedirectssetRequestMethodusingProxy

Field Detail

defaultHostnameVerifierback to summary
private static HostnameVerifier defaultHostnameVerifier

HostnameVerifier provides a callback mechanism so that implementers of this interface can supply a policy for handling the case where the host to connect to and the server name from the certificate mismatch.

The default implementation will deny such connections.

defaultSSLSocketFactoryback to summary
private static SSLSocketFactory defaultSSLSocketFactory
hostnameVerifierback to summary
protected HostnameVerifier hostnameVerifier

The hostnameVerifier for this object.

sslSocketFactoryback to summary
private SSLSocketFactory sslSocketFactory

The SSLSocketFactory inherited when an instance of this class is created.

Constructor Detail

HttpsURLConnectionback to summary
protected HttpsURLConnection(URL url)

Creates an HttpsURLConnection using the URL specified.

Parameters
url:URL

the URL

Method Detail

getCipherSuiteback to summary
public abstract String getCipherSuite()

Returns the cipher suite in use on this connection.

Returns:String

the cipher suite

Exceptions
IllegalStateException:
if this method is called before the connection has been established.
getDefaultHostnameVerifierback to summary
public static HostnameVerifier getDefaultHostnameVerifier()

Gets the default HostnameVerifier that is inherited by new instances of this class.

Returns:HostnameVerifier

the default host name verifier

See Also
setDefaultHostnameVerifier(HostnameVerifier)
getDefaultSSLSocketFactoryback to summary
public static SSLSocketFactory getDefaultSSLSocketFactory()

Gets the default static SSLSocketFactory that is inherited by new instances of this class.

The socket factories are used when creating sockets for secure https URL connections.

Returns:SSLSocketFactory

the default SSLSocketFactory

See Also
setDefaultSSLSocketFactory(SSLSocketFactory)
getHostnameVerifierback to summary
public HostnameVerifier getHostnameVerifier()

Gets the HostnameVerifier in place on this instance.

Returns:HostnameVerifier

the host name verifier

See Also
setHostnameVerifier(HostnameVerifier), setDefaultHostnameVerifier(HostnameVerifier)
getLocalCertificatesback to summary
public abstract Certificate[] getLocalCertificates()

Returns the certificate(s) that were sent to the server during handshaking.

Note

This method is useful only when using certificate-based cipher suites.

When multiple certificates are available for use in a handshake, the implementation chooses what it considers the "best" certificate chain available, and transmits that to the other side. This method allows the caller to know which certificate chain was actually sent.

Returns:Certificate[]

an ordered array of certificates, with the client's own certificate first followed by any certificate authorities. If no certificates were sent, then null is returned.

Exceptions
IllegalStateException:
if this method is called before the connection has been established.
See Also
getLocalPrincipal()
getLocalPrincipalback to summary
public Principal getLocalPrincipal()

Returns the principal that was sent to the server during handshaking.

Note

Subclasses should override this method. If not overridden, it will default to returning the X500Principal of the end-entity certificate that was sent to the server for certificate-based ciphersuites or, return null for non-certificate based ciphersuites, such as Kerberos.

Returns:Principal

the principal sent to the server. Returns an X500Principal of the end-entity certificate for X509-based cipher suites, and KerberosPrincipal for Kerberos cipher suites. If no principal was sent, then null is returned.

Exceptions
IllegalStateException:
if this method is called before the connection has been established.
Since
1.5
See Also
getLocalCertificates(), getPeerPrincipal()
getPeerPrincipalback to summary
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException

Returns the server's principal which was established as part of defining the session.

Note

Subclasses should override this method. If not overridden, it will default to returning the X500Principal of the server's end-entity certificate for certificate-based ciphersuites, or throw an SSLPeerUnverifiedException for non-certificate based ciphersuites, such as Kerberos.

Returns:Principal

the server's principal. Returns an X500Principal of the end-entity certificate for X509-based cipher suites, and KerberosPrincipal for Kerberos cipher suites.

Exceptions
SSLPeerUnverifiedException:
if the peer was not verified
IllegalStateException:
if this method is called before the connection has been established.
Since
1.5
See Also
getServerCertificates(), getLocalPrincipal()
getServerCertificatesback to summary
public abstract Certificate[] getServerCertificates() throws SSLPeerUnverifiedException

Returns the server's certificate chain which was established as part of defining the session.

Note

This method can be used only when using certificate-based cipher suites; using it with non-certificate-based cipher suites, such as Kerberos, will throw an SSLPeerUnverifiedException.

Note

The returned value may not be a valid certificate chain and should not be relied on for trust decisions.

Returns:Certificate[]

an ordered array of server certificates, with the peer's own certificate first followed by any certificate authorities.

Exceptions
SSLPeerUnverifiedException:
if the peer is not verified.
IllegalStateException:
if this method is called before the connection has been established.
See Also
getPeerPrincipal()
getSSLSessionback to summary
public Optional<SSLSession> getSSLSession()

Returns an Optional containing the SSLSession in use on this connection. Returns an empty Optional if the underlying implementation does not support this method.

Implementation Specification

For compatibility, the default implementation of this method returns an empty Optional. Subclasses should override this method with an appropriate implementation since an application may need to access additional parameters associated with the SSL session.

Returns:Optional<SSLSession>

an Optional containing the SSLSession in use on this connection.

Exceptions
IllegalStateException:
if this method is called before the connection has been established
Since
12
See Also
SSLSession
getSSLSocketFactoryback to summary
public SSLSocketFactory getSSLSocketFactory()

Gets the SSL socket factory to be used when creating sockets for secure https URL connections.

Returns:SSLSocketFactory

the SSLSocketFactory

See Also
setSSLSocketFactory(SSLSocketFactory)
setDefaultHostnameVerifierback to summary
public static void setDefaultHostnameVerifier(HostnameVerifier v)

Sets the default HostnameVerifier inherited by a new instance of this class.

If this method is not called, the default HostnameVerifier assumes the connection should not be permitted.

Parameters
v:HostnameVerifier

the default host name verifier

Exceptions
IllegalArgumentException:
if the HostnameVerifier parameter is null.
SecurityException:
if a security manager exists and its checkPermission method does not allow SSLPermission("setHostnameVerifier")
See Also
getDefaultHostnameVerifier()
setDefaultSSLSocketFactoryback to summary
public static void setDefaultSSLSocketFactory(SSLSocketFactory sf)

Sets the default SSLSocketFactory inherited by new instances of this class.

The socket factories are used when creating sockets for secure https URL connections.

Parameters
sf:SSLSocketFactory

the default SSL socket factory

Exceptions
IllegalArgumentException:
if the SSLSocketFactory parameter is null.
SecurityException:
if a security manager exists and its checkSetFactory method does not allow a socket factory to be specified.
See Also
getDefaultSSLSocketFactory()
setHostnameVerifierback to summary
public void setHostnameVerifier(HostnameVerifier v)

Sets the HostnameVerifier for this instance.

New instances of this class inherit the default static hostname verifier set by setDefaultHostnameVerifier. Calls to this method replace this object's HostnameVerifier.

Parameters
v:HostnameVerifier

the host name verifier

Exceptions
IllegalArgumentException:
if the HostnameVerifier parameter is null.
See Also
getHostnameVerifier(), setDefaultHostnameVerifier(HostnameVerifier)
setSSLSocketFactoryback to summary
public void setSSLSocketFactory(SSLSocketFactory sf)

Sets the SSLSocketFactory to be used when this instance creates sockets for secure https URL connections.

New instances of this class inherit the default static SSLSocketFactory set by setDefaultSSLSocketFactory. Calls to this method replace this object's SSLSocketFactory.

Parameters
sf:SSLSocketFactory

the SSL socket factory

Exceptions
IllegalArgumentException:
if the SSLSocketFactory parameter is null.
SecurityException:
if a security manager exists and its checkSetFactory method does not allow a socket factory to be specified.
See Also
getSSLSocketFactory()
javax.net.ssl back to summary

private Class HttpsURLConnection.DefaultHostnameVerifier

extends Object
implements HostnameVerifier
Class Inheritance
All Implemented Interfaces
javax.net.ssl.HostnameVerifier

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
verify(String
the host name
hostname
,
SSLSession
SSLSession used on the connection to host
session
)

Implements javax.net.ssl.HostnameVerifier.verify.

Verify that the host name is an acceptable match with the server's authentication scheme.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DefaultHostnameVerifierback to summary
private DefaultHostnameVerifier()

Method Detail

verifyback to summary
public boolean verify(String hostname, SSLSession session)

Implements javax.net.ssl.HostnameVerifier.verify.

Doc from javax.net.ssl.HostnameVerifier.verify.

Verify that the host name is an acceptable match with the server's authentication scheme.

Parameters
hostname:String

the host name

session:SSLSession

SSLSession used on the connection to host

Returns:boolean

true if the host name is acceptable

Annotations
@Override