Class HttpsURLConnection
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 HttpsURLConnection
s 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
connect
ing.
- Since:
- 1.4
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected HostnameVerifier
ThehostnameVerifier
for this object.Fields declared in class 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
Modifier and TypeFieldDescriptionprotected int
The chunk-length when using chunked encoding streaming mode for output.protected int
The fixed content-length when using fixed-length streaming mode.protected long
The fixed content-length when using fixed-length streaming mode.static final int
HTTP Status-Code 202: Accepted.static final int
HTTP Status-Code 502: Bad Gateway.static final int
HTTP Status-Code 405: Method Not Allowed.static final int
HTTP Status-Code 400: Bad Request.static final int
HTTP Status-Code 408: Request Time-Out.static final int
HTTP Status-Code 409: Conflict.static final int
HTTP Status-Code 201: Created.static final int
HTTP Status-Code 413: Request Entity Too Large.static final int
HTTP Status-Code 403: Forbidden.static final int
HTTP Status-Code 504: Gateway Timeout.static final int
HTTP Status-Code 410: Gone.static final int
HTTP Status-Code 500: Internal Server Error.static final int
HTTP Status-Code 411: Length Required.static final int
HTTP Status-Code 301: Moved Permanently.static final int
HTTP Status-Code 302: Temporary Redirect.static final int
HTTP Status-Code 300: Multiple Choices.static final int
HTTP Status-Code 204: No Content.static final int
HTTP Status-Code 406: Not Acceptable.static final int
HTTP Status-Code 203: Non-Authoritative Information.static final int
HTTP Status-Code 404: Not Found.static final int
HTTP Status-Code 501: Not Implemented.static final int
HTTP Status-Code 304: Not Modified.static final int
HTTP Status-Code 200: OK.static final int
HTTP Status-Code 206: Partial Content.static final int
HTTP Status-Code 402: Payment Required.static final int
HTTP Status-Code 412: Precondition Failed.static final int
HTTP Status-Code 407: Proxy Authentication Required.static final int
HTTP Status-Code 414: Request-URI Too Large.static final int
HTTP Status-Code 205: Reset Content.static final int
HTTP Status-Code 303: See Other.static final int
Deprecated.it is misplaced and shouldn't have existed.static final int
HTTP Status-Code 401: Unauthorized.static final int
HTTP Status-Code 503: Service Unavailable.static final int
HTTP Status-Code 415: Unsupported Media Type.static final int
HTTP Status-Code 305: Use Proxy.static final int
HTTP Status-Code 505: HTTP Version Not Supported.protected boolean
Iftrue
, the protocol will automatically follow redirects.protected String
The HTTP method (GET,POST,PUT,etc.).protected int
Anint
representing the three digit HTTP Status-Code.protected String
The HTTP response message.Fields declared in class URLConnection
allowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches
Modifier and TypeFieldDescriptionprotected boolean
Iftrue
, thisURL
is being examined in a context in which it makes sense to allow user interactions such as popping up an authentication dialog.protected boolean
Iffalse
, this connection object has not created a communications link to the specified URL.protected boolean
This variable is set by thesetDoInput
method.protected boolean
This variable is set by thesetDoOutput
method.protected long
Some protocols support skipping the fetching of the object unless the object has been modified more recently than a certain time.protected URL
The URL represents the remote object on the World Wide Web to which this connection is opened.protected boolean
Iftrue
, the protocol is allowed to use caching whenever it can. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
HttpsURLConnection
(URL url) Creates anHttpsURLConnection
using the URL specified. -
Method Summary
Modifier and TypeMethodDescriptionabstract String
Returns the cipher suite in use on this connection.static HostnameVerifier
Gets the defaultHostnameVerifier
that is inherited by new instances of this class.static SSLSocketFactory
Gets the default staticSSLSocketFactory
that is inherited by new instances of this class.Gets theHostnameVerifier
in place on this instance.abstract Certificate[]
Returns the certificate(s) that were sent to the server during handshaking.Returns the principal that was sent to the server during handshaking.Returns the server's principal which was established as part of defining the session.abstract Certificate[]
Returns the server's certificate chain which was established as part of defining the session.Returns anOptional
containing theSSLSession
in use on this connection.Gets the SSL socket factory to be used when creating sockets for secure https URL connections.static void
Sets the defaultHostnameVerifier
inherited by a new instance of this class.static void
Sets the defaultSSLSocketFactory
inherited by new instances of this class.void
Sets theHostnameVerifier
for this instance.void
Sets theSSLSocketFactory
to be used when this instance creates sockets for secure https URL connections.Methods declared in class HttpURLConnection
disconnect, getErrorStream, getFollowRedirects, getHeaderField, getHeaderFieldKey, getInstanceFollowRedirects, getPermission, getRequestMethod, getResponseCode, getResponseMessage, setAuthenticator, setChunkedStreamingMode, setFixedLengthStreamingMode, setFixedLengthStreamingMode, setFollowRedirects, setInstanceFollowRedirects, setRequestMethod, usingProxy
Modifier and TypeMethodDescriptionabstract void
Indicates that other requests to the server are unlikely in the near future.Returns the error stream if the connection failed but the server sent useful data nonetheless.static boolean
Returns aboolean
indicating whether or not HTTP redirects (3xx) should be automatically followed.getHeaderField
(int n) Returns the value for then
th header field.getHeaderFieldKey
(int n) Returns the key for then
th header field.boolean
Returns the value of thisHttpURLConnection
'sinstanceFollowRedirects
field.Deprecated, for removal: This API element is subject to removal in a future version.Permissions can no longer be used for controlling access to resources as the Security Manager is no longer supported.Get the request method.int
Gets the status code from an HTTP response message.Gets the HTTP response message, if any, returned along with the response code from a server.void
Supplies anAuthenticator
to be used when authentication is requested through the HTTP protocol for thisHttpURLConnection
.void
setChunkedStreamingMode
(int chunklen) This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance.void
setFixedLengthStreamingMode
(int contentLength) This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.void
setFixedLengthStreamingMode
(long contentLength) This method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.static void
setFollowRedirects
(boolean set) Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class.void
setInstanceFollowRedirects
(boolean followRedirects) Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by thisHttpURLConnection
instance.void
setRequestMethod
(String method) Set the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions.abstract boolean
Indicates if the connection is going through a proxy.Methods declared in class URLConnection
addRequestProperty, connect, getAllowUserInteraction, getConnectTimeout, getContent, getContent, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderField, getHeaderFieldDate, getHeaderFieldInt, getHeaderFieldLong, getHeaderFields, getIfModifiedSince, getInputStream, getLastModified, getOutputStream, getReadTimeout, getRequestProperties, getRequestProperty, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setConnectTimeout, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setIfModifiedSince, setReadTimeout, setRequestProperty, setUseCaches, toString
Modifier and TypeMethodDescriptionvoid
addRequestProperty
(String key, String value) Adds a general request property specified by a key-value pair.abstract void
connect()
Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.boolean
Returns the value of theallowUserInteraction
field for this object.int
Returns setting for connect timeout.Retrieves the contents of this URL connection.getContent
(Class<?>[] classes) Retrieves the contents of this URL connection.Returns the value of thecontent-encoding
header field.int
Returns the value of thecontent-length
header field.long
Returns the value of thecontent-length
header field as a long.Returns the value of thecontent-type
header field.long
getDate()
Returns the value of thedate
header field.static boolean
Returns the default value of theallowUserInteraction
field.static String
Deprecated.The instance specific getRequestProperty method should be used after an appropriate instance of URLConnection is obtained.boolean
Returns the default value of aURLConnection
'suseCaches
flag.static boolean
getDefaultUseCaches
(String protocol) Returns the default value of theuseCaches
flag for the given protocol.boolean
Returns the value of thisURLConnection
'sdoInput
flag.boolean
Returns the value of thisURLConnection
'sdoOutput
flag.long
Returns the value of theexpires
header field.static FileNameMap
Loads filename map (a mimetable) from a data file.getHeaderField
(String name) Returns the value of the named header field.long
getHeaderFieldDate
(String name, long defaultValue) Returns the value of the named field parsed as date.int
getHeaderFieldInt
(String name, int defaultValue) Returns the value of the named field parsed as a number.long
getHeaderFieldLong
(String name, long defaultValue) Returns the value of the named field parsed as a number.Returns an unmodifiable Map of the header fields.long
Returns the value of this object'sifModifiedSince
field.Returns an input stream that reads from this open connection.long
Returns the value of thelast-modified
header field.Returns an output stream that writes to this connection.int
Returns setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).Returns an unmodifiable Map of general request properties for this connection.getRequestProperty
(String key) Returns the value of the named general request property for this connection.getURL()
Returns the value of thisURLConnection
'sURL
field.boolean
Returns the value of thisURLConnection
'suseCaches
field.static String
guessContentTypeFromName
(String fname) Tries to determine the content type of an object, based on the specified "file" component of a URL.static String
Tries to determine the type of an input stream based on the characters at the beginning of the input stream.void
setAllowUserInteraction
(boolean allowuserinteraction) Set the value of theallowUserInteraction
field of thisURLConnection
.void
setConnectTimeout
(int timeout) Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.static void
Sets theContentHandlerFactory
of an application.static void
setDefaultAllowUserInteraction
(boolean defaultallowuserinteraction) Sets the default value of theallowUserInteraction
field for all futureURLConnection
objects to the specified value.static void
setDefaultRequestProperty
(String key, String value) Deprecated.The instance specific setRequestProperty method should be used after an appropriate instance of URLConnection is obtained.void
setDefaultUseCaches
(boolean defaultusecaches) Sets the default value of theuseCaches
field to the specified value.static void
setDefaultUseCaches
(String protocol, boolean defaultVal) Sets the default value of theuseCaches
field for the named protocol to the given value.void
setDoInput
(boolean doinput) Sets the value of thedoInput
field for thisURLConnection
to the specified value.void
setDoOutput
(boolean dooutput) Sets the value of thedoOutput
field for thisURLConnection
to the specified value.static void
Sets the FileNameMap.void
setIfModifiedSince
(long ifmodifiedsince) Sets the value of theifModifiedSince
field of thisURLConnection
to the specified value.void
setReadTimeout
(int timeout) Sets the read timeout to a specified timeout, in milliseconds.void
setRequestProperty
(String key, String value) Sets the general request property.void
setUseCaches
(boolean usecaches) Sets the value of theuseCaches
field of thisURLConnection
to the specified value.toString()
Returns aString
representation of this URL connection.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Modifier and TypeMethodDescriptionprotected Object
clone()
Creates and returns a copy of this object.boolean
Indicates whether some other object is "equal to" this one.protected void
finalize()
Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class
<?> getClass()
Returns the runtime class of thisObject
.int
hashCode()
Returns a hash code value for this object.final void
notify()
Wakes up a single thread that is waiting on this object's monitor.final void
Wakes up all threads that are waiting on this object's monitor.final void
wait()
Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final void
wait
(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final void
wait
(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
-
Field Details
-
hostnameVerifier
ThehostnameVerifier
for this object.
-
-
Constructor Details
-
HttpsURLConnection
Creates anHttpsURLConnection
using the URL specified.- Parameters:
url
- the URL
-
-
Method Details
-
getCipherSuite
Returns the cipher suite in use on this connection.- Returns:
- the cipher suite
- Throws:
IllegalStateException
- if this method is called before the connection has been established.
-
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:
- 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.
- Throws:
IllegalStateException
- if this method is called before the connection has been established.- See Also:
-
getServerCertificates
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:
- an ordered array of server certificates, with the peer's own certificate first followed by any certificate authorities.
- Throws:
SSLPeerUnverifiedException
- if the peer is not verified.IllegalStateException
- if this method is called before the connection has been established.- See Also:
-
getPeerPrincipal
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:
- the server's principal. Returns an X500Principal of the end-entity certificate for X509-based cipher suites, and KerberosPrincipal for Kerberos cipher suites.
- Throws:
SSLPeerUnverifiedException
- if the peer was not verifiedIllegalStateException
- if this method is called before the connection has been established.- Since:
- 1.5
- See Also:
-
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:
- 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.
- Throws:
IllegalStateException
- if this method is called before the connection has been established.- Since:
- 1.5
- See Also:
-
setDefaultHostnameVerifier
Sets the defaultHostnameVerifier
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
- the default host name verifier- Throws:
IllegalArgumentException
- if theHostnameVerifier
parameter is null.- See Also:
-
getDefaultHostnameVerifier
Gets the defaultHostnameVerifier
that is inherited by new instances of this class.- Returns:
- the default host name verifier
- See Also:
-
setHostnameVerifier
Sets theHostnameVerifier
for this instance.New instances of this class inherit the default static hostname verifier set by
setDefaultHostnameVerifier
. Calls to this method replace this object'sHostnameVerifier
.- Parameters:
v
- the host name verifier- Throws:
IllegalArgumentException
- if theHostnameVerifier
parameter is null.- See Also:
-
getHostnameVerifier
Gets theHostnameVerifier
in place on this instance.- Returns:
- the host name verifier
- See Also:
-
setDefaultSSLSocketFactory
Sets the defaultSSLSocketFactory
inherited by new instances of this class.The socket factories are used when creating sockets for secure https URL connections.
- Parameters:
sf
- the default SSL socket factory- Throws:
IllegalArgumentException
- if the SSLSocketFactory parameter is null.- See Also:
-
getDefaultSSLSocketFactory
Gets the default staticSSLSocketFactory
that is inherited by new instances of this class.The socket factories are used when creating sockets for secure https URL connections.
- Returns:
- the default
SSLSocketFactory
- See Also:
-
setSSLSocketFactory
Sets theSSLSocketFactory
to be used when this instance creates sockets for secure https URL connections.New instances of this class inherit the default static
SSLSocketFactory
set bysetDefaultSSLSocketFactory
. Calls to this method replace this object'sSSLSocketFactory
.- Parameters:
sf
- the SSL socket factory- Throws:
IllegalArgumentException
- if theSSLSocketFactory
parameter is null.- See Also:
-
getSSLSocketFactory
Gets the SSL socket factory to be used when creating sockets for secure https URL connections.- Returns:
- the
SSLSocketFactory
- See Also:
-
getSSLSession
Returns anOptional
containing theSSLSession
in use on this connection. Returns an emptyOptional
if the underlying implementation does not support this method.- Implementation Requirements:
- 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:
- an
Optional
containing theSSLSession
in use on this connection. - Throws:
IllegalStateException
- if this method is called before the connection has been established- Since:
- 12
- See Also:
-