public final class HttpsURLConnectionImpl extends HttpsURLConnection
hostnameVerifierchunkLength, 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, responseMessageallowUserInteraction, connected, doInput, doOutput, ifModifiedSince, url, useCaches| Constructor and Description |
|---|
HttpsURLConnectionImpl(HttpURLConnectionImpl delegate) |
HttpsURLConnectionImpl(URL url,
OkHttpClient client) |
| Modifier and Type | Method and Description |
|---|---|
void |
addRequestProperty(String field,
String newValue)
Adds the given property to the request header.
|
void |
connect()
Opens a connection to the resource.
|
void |
disconnect()
Releases this connection so that its resources may be either reused or
closed.
|
boolean |
getAllowUserInteraction()
Returns
allowUserInteraction. |
String |
getCipherSuite()
Returns the name of the cipher suite negotiated during the SSL handshake.
|
int |
getConnectTimeout()
Returns the connect timeout in milliseconds.
|
Object |
getContent()
Returns an object representing the content of the resource this
URLConnection is connected to. |
Object |
getContent(Class[] types)
Returns an object representing the content of the resource this
URLConnection is connected to. |
String |
getContentEncoding()
Returns the encoding used to transmit the response body over the network.
|
int |
getContentLength()
Returns the content length in bytes specified by the response header field
content-length or -1 if this field is not set or cannot be represented as an
int. |
String |
getContentType()
Returns the MIME-type of the content specified by the response header field
content-type or null if type is unknown. |
long |
getDate()
Returns the timestamp when this response has been sent as a date in
milliseconds since January 1, 1970 GMT or
0 if this timestamp is
unknown. |
boolean |
getDefaultUseCaches()
Returns the default setting whether this connection allows using caches.
|
boolean |
getDoInput()
Returns the value of the option
doInput which specifies whether this
connection allows to receive data. |
boolean |
getDoOutput()
Returns the value of the option
doOutput which specifies whether
this connection allows to send data. |
InputStream |
getErrorStream()
Returns an input stream from the server in the case of an error such as
the requested file has not been found on the remote server.
|
long |
getExpiration()
Returns the timestamp when this response will be expired in milliseconds
since January 1, 1970 GMT or
0 if this timestamp is unknown. |
String |
getHeaderField(int pos)
Returns the header value at the field position
pos or null
if the header has fewer than pos fields. |
String |
getHeaderField(String key)
Returns the value of the header field specified by
key or null if there is no field with this name. |
long |
getHeaderFieldDate(String field,
long defaultValue)
Returns the date value in milliseconds since
01.01.1970, 00:00h
corresponding to the header field field. |
int |
getHeaderFieldInt(String field,
int defaultValue)
Returns the specified header value as a number.
|
String |
getHeaderFieldKey(int position)
Returns the name of the header field at the given position
posn or
null if there are fewer than posn fields. |
Map<String,List<String>> |
getHeaderFields()
Returns an unmodifiable map of the response-header fields and values.
|
HostnameVerifier |
getHostnameVerifier()
Returns the hostname verifier used by this instance.
|
long |
getIfModifiedSince()
Returns the point of time since when the data must be modified to be
transmitted.
|
InputStream |
getInputStream()
Returns an
InputStream for reading data from the resource pointed by
this URLConnection. |
boolean |
getInstanceFollowRedirects()
Returns whether this connection follows redirects.
|
long |
getLastModified()
Returns the value of the response header field
last-modified or
0 if this value is not set. |
Certificate[] |
getLocalCertificates()
Returns the list of local certificates used during the handshake.
|
Principal |
getLocalPrincipal()
Returns the
Principal used to identify the local host during the handshake. |
OutputStream |
getOutputStream()
Returns an
OutputStream for writing data to this URLConnection. |
Principal |
getPeerPrincipal()
Returns the
Principal identifying the peer. |
Permission |
getPermission()
Returns the permission object (in this case
SocketPermission)
with the host and the port number as the target name and "resolve, connect" as the action list. |
int |
getReadTimeout()
Returns the read timeout in milliseconds, or
0 if reads never
timeout. |
String |
getRequestMethod()
Returns the request method which will be used to make the request to the
remote HTTP server.
|
Map<String,List<String>> |
getRequestProperties()
Returns an unmodifiable map of general request properties used by this
connection.
|
String |
getRequestProperty(String field)
Returns the value of the request header property specified by {code field}
or
null if there is no field with this name. |
int |
getResponseCode()
Returns the response code returned by the remote HTTP server.
|
String |
getResponseMessage()
Returns the response message returned by the remote HTTP server.
|
Certificate[] |
getServerCertificates()
Return the list of certificates identifying the peer during the
handshake.
|
SSLSocketFactory |
getSSLSocketFactory()
Returns the SSL socket factory used by this instance.
|
URL |
getURL()
Returns the URL represented by this
URLConnection. |
boolean |
getUseCaches()
Returns the value of the flag which specifies whether this
URLConnection allows to use caches. |
protected Handshake |
handshake() |
void |
setAllowUserInteraction(boolean newValue)
Sets
allowUserInteraction. |
void |
setChunkedStreamingMode(int chunkLength)
Stream a request body whose length is not known in advance.
|
void |
setConnectTimeout(int timeoutMillis)
Sets the maximum time in milliseconds to wait while connecting.
|
void |
setDefaultUseCaches(boolean newValue)
Sets the default value for the flag indicating whether this connection
allows to use caches.
|
void |
setDoInput(boolean newValue)
Sets the flag indicating whether this
URLConnection allows input. |
void |
setDoOutput(boolean newValue)
Sets the flag indicating whether this
URLConnection allows
output. |
void |
setFixedLengthStreamingMode(int contentLength)
Equivalent to
setFixedLengthStreamingMode((long) contentLength),
but available on earlier versions of Android and limited to 2 GiB. |
void |
setFixedLengthStreamingMode(long contentLength)
Configures this connection to stream the request body with the known
fixed byte count of
contentLength. |
void |
setHostnameVerifier(HostnameVerifier hostnameVerifier)
Sets the hostname verifier for this instance.
|
void |
setIfModifiedSince(long newValue)
Sets the point of time since when the data must be modified to be
transmitted.
|
void |
setInstanceFollowRedirects(boolean followRedirects)
Sets whether this connection follows redirects.
|
void |
setReadTimeout(int timeoutMillis)
Sets the maximum time to wait for an input stream read to complete before
giving up.
|
void |
setRequestMethod(String method)
Sets the request command which will be sent to the remote HTTP server.
|
void |
setRequestProperty(String field,
String newValue)
Sets the value of the specified request header field.
|
void |
setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
Sets the SSL socket factory for this instance.
|
void |
setUseCaches(boolean newValue)
Sets the flag indicating whether this connection allows to use caches or
not.
|
String |
toString()
Returns the string representation containing the name of this class and
the URL.
|
boolean |
usingProxy()
Returns whether this connection uses a proxy server or not.
|
getDefaultHostnameVerifier, getDefaultSSLSocketFactory, setDefaultHostnameVerifier, setDefaultSSLSocketFactorygetFollowRedirects, setFollowRedirectspublic HttpsURLConnectionImpl(URL url, OkHttpClient client)
public HttpsURLConnectionImpl(HttpURLConnectionImpl delegate)
protected Handshake handshake()
public void setHostnameVerifier(HostnameVerifier hostnameVerifier)
HttpsURLConnectionhostnameVerifier - the hostname verifier for this instance.public HostnameVerifier getHostnameVerifier()
HttpsURLConnectionpublic void setSSLSocketFactory(SSLSocketFactory sslSocketFactory)
HttpsURLConnectionsslSocketFactory - the SSL socket factory to be used by this instance.public SSLSocketFactory getSSLSocketFactory()
HttpsURLConnectionpublic void setFixedLengthStreamingMode(long contentLength)
HttpURLConnectioncontentLength.setFixedLengthStreamingMode in class HttpURLConnectioncontentLength - the fixed length of the HTTP request body.HttpURLConnection.setChunkedStreamingMode(int)public String getCipherSuite()
HttpsURLConnectiongetCipherSuite in class HttpsURLConnectionpublic Certificate[] getLocalCertificates()
HttpsURLConnectiongetLocalCertificates in class HttpsURLConnectionnull
if no certificates were used during the handshake.public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException
HttpsURLConnectiongetServerCertificates in class HttpsURLConnectionSSLPeerUnverifiedException - if the identity of the peer has not been verified..public Principal getPeerPrincipal() throws SSLPeerUnverifiedException
HttpsURLConnectionPrincipal identifying the peer.getPeerPrincipal in class HttpsURLConnectionPrincipal identifying the peer.SSLPeerUnverifiedException - if the identity of the peer has not been verified.public Principal getLocalPrincipal()
HttpsURLConnectionPrincipal used to identify the local host during the handshake.getLocalPrincipal in class HttpsURLConnectionPrincipal used to identify the local host during the handshake, or
null if none was used.public void connect()
throws IOException
URLConnectionconnect in class URLConnectionIOException - if an error occurs while connecting to the resource.public void disconnect()
HttpURLConnectionUnlike other Java implementations, this will not necessarily close
socket connections that can be reused. You can disable all connection
reuse by setting the http.keepAlive system property to false before issuing any HTTP requests.
disconnect in class HttpURLConnectionpublic InputStream getErrorStream()
HttpURLConnectiongetErrorStream in class HttpURLConnectionpublic String getRequestMethod()
HttpURLConnectiongetRequestMethod in class HttpURLConnectionHttpURLConnection.method,
HttpURLConnection.setRequestMethod(java.lang.String)public int getResponseCode()
throws IOException
HttpURLConnectiongetResponseCode in class HttpURLConnectionIOException - if there is an IO error during the retrieval.HttpURLConnection.getResponseMessage()public String getResponseMessage() throws IOException
HttpURLConnectiongetResponseMessage in class HttpURLConnectionnull if no such response exists.IOException - if there is an error during the retrieval.HttpURLConnection.getResponseCode()public void setRequestMethod(String method) throws ProtocolException
HttpURLConnectionsetRequestMethod in class HttpURLConnectionmethod - the string representing the method to be used.ProtocolException - if this is called after connected, or the method is not
supported by this HTTP implementation.HttpURLConnection.getRequestMethod(),
HttpURLConnection.methodpublic boolean usingProxy()
HttpURLConnectionusingProxy in class HttpURLConnectiontrue if this connection passes a proxy server, false
otherwise.public boolean getInstanceFollowRedirects()
HttpURLConnectiongetInstanceFollowRedirects in class HttpURLConnectiontrue if this connection follows redirects, false
otherwise.public void setInstanceFollowRedirects(boolean followRedirects)
HttpURLConnectionsetInstanceFollowRedirects in class HttpURLConnectionfollowRedirects - true if this connection will follows redirects, false
otherwise.public boolean getAllowUserInteraction()
URLConnectionallowUserInteraction. Unused by Android.getAllowUserInteraction in class URLConnectionpublic Object getContent() throws IOException
URLConnectionURLConnection is connected to. First, it attempts to get the content
type from the method getContentType() which looks at the response
header field "Content-Type". If none is found it will guess the content
type from the filename extension. If that fails the stream itself will be
used to guess the content type.getContent in class URLConnectionIOException - if an error occurs obtaining the content.public Object getContent(Class[] types) throws IOException
URLConnectionURLConnection is connected to. First, it attempts to get the content
type from the method getContentType() which looks at the response
header field "Content-Type". If none is found it will guess the content
type from the filename extension. If that fails the stream itself will be
used to guess the content type. The content type must match with one of
the list types.getContent in class URLConnectiontypes - the list of acceptable content types.null if the content
type does not match with one of the specified types.IOException - if an error occurs obtaining the content.public String getContentEncoding()
HttpURLConnectioncontent type, which may also include the
content's character encoding.getContentEncoding in class HttpURLConnectioncontent-encoding.public int getContentLength()
URLConnectioncontent-length or -1 if this field is not set or cannot be represented as an
int.getContentLength in class URLConnectionpublic String getContentType()
URLConnectioncontent-type or null if type is unknown.getContentType in class URLConnectioncontent-type.public long getDate()
URLConnection0 if this timestamp is
unknown.getDate in class URLConnectionpublic boolean getDefaultUseCaches()
URLConnectiongetDefaultUseCaches in class URLConnectiondefaultUseCaches.URLConnection.useCachespublic boolean getDoInput()
URLConnectiondoInput which specifies whether this
connection allows to receive data.getDoInput in class URLConnectiontrue if this connection allows input, false
otherwise.URLConnection.doInputpublic boolean getDoOutput()
URLConnectiondoOutput which specifies whether
this connection allows to send data.getDoOutput in class URLConnectiontrue if this connection allows output, false
otherwise.URLConnection.doOutputpublic long getExpiration()
URLConnection0 if this timestamp is unknown.getExpiration in class URLConnectionexpires.public String getHeaderField(int pos)
URLConnectionpos or null
if the header has fewer than pos fields. The base
implementation of this method returns always null.
Some implementations (notably HttpURLConnection) include a mapping
for the null key; in HTTP's case, this maps to the HTTP status line and is
treated as being at position 0 when indexing into the header fields.
getHeaderField in class URLConnectionpos - the field position of the response header.pos.public Map<String,List<String>> getHeaderFields()
URLConnectionSome implementations (notably HttpURLConnection) include a mapping
for the null key; in HTTP's case, this maps to the HTTP status line and is
treated as being at position 0 when indexing into the header fields.
getHeaderFields in class URLConnectionpublic Map<String,List<String>> getRequestProperties()
URLConnectiongetRequestProperties in class URLConnectionpublic void addRequestProperty(String field, String newValue)
URLConnectionaddRequestProperty in class URLConnectionfield - the request property field name to add.newValue - the value of the property which is to add.public String getHeaderField(String key)
URLConnectionkey or null if there is no field with this name. The base implementation of
this method returns always null.
Some implementations (notably HttpURLConnection) include a mapping
for the null key; in HTTP's case, this maps to the HTTP status line and is
treated as being at position 0 when indexing into the header fields.
getHeaderField in class URLConnectionkey - the name of the header field.public long getHeaderFieldDate(String field, long defaultValue)
HttpURLConnection01.01.1970, 00:00h
corresponding to the header field field. The defaultValue
will be returned if no such field can be found in the response header.getHeaderFieldDate in class HttpURLConnectionfield - the header field name.defaultValue - the default value to use if the specified header field wont be
found.public int getHeaderFieldInt(String field, int defaultValue)
URLConnectiondefaultValue if no such header field could be found or the value could
not be parsed as an int.getHeaderFieldInt in class URLConnectionfield - the header field name whose value is needed.defaultValue - the default value if no field has been found.public String getHeaderFieldKey(int position)
URLConnectionposn or
null if there are fewer than posn fields. The base
implementation of this method returns always null.
Some implementations (notably HttpURLConnection) include a mapping
for the null key; in HTTP's case, this maps to the HTTP status line and is
treated as being at position 0 when indexing into the header fields.
getHeaderFieldKey in class URLConnectionposition - the position of the header field which has to be returned.public long getIfModifiedSince()
URLConnectiongetIfModifiedSince in class URLConnectionURLConnection.ifModifiedSincepublic InputStream getInputStream() throws IOException
URLConnectionInputStream for reading data from the resource pointed by
this URLConnection. It throws an UnknownServiceException by
default. This method must be overridden by its subclasses.getInputStream in class URLConnectionIOException - if no InputStream could be created.public long getLastModified()
URLConnectionlast-modified or
0 if this value is not set.getLastModified in class URLConnectionlast-modified header field.public OutputStream getOutputStream() throws IOException
URLConnectionOutputStream for writing data to this URLConnection. It throws an UnknownServiceException by default.
This method must be overridden by its subclasses.getOutputStream in class URLConnectionIOException - if no OutputStream could be created.public Permission getPermission() throws IOException
HttpURLConnectionSocketPermission)
with the host and the port number as the target name and "resolve, connect" as the action list. If the port number of this URL
instance is lower than 0 the port will be set to 80.getPermission in class HttpURLConnectionIOException - if an IO exception occurs during the creation of the
permission object.public String getRequestProperty(String field)
URLConnectionnull if there is no field with this name. The base
implementation of this method returns always null.getRequestProperty in class URLConnectionfield - the name of the request header property.public URL getURL()
URLConnectionURLConnection.getURL in class URLConnectionpublic boolean getUseCaches()
URLConnectionURLConnection allows to use caches.getUseCaches in class URLConnectiontrue if using caches is allowed, false otherwise.public void setAllowUserInteraction(boolean newValue)
URLConnectionallowUserInteraction. Unused by Android.setAllowUserInteraction in class URLConnectionpublic void setDefaultUseCaches(boolean newValue)
URLConnectionURLConnections are unaffected.setDefaultUseCaches in class URLConnectionnewValue - the default value of the flag to be used for new connections.URLConnection.useCachespublic void setDoInput(boolean newValue)
URLConnectionURLConnection allows input.
It cannot be set after the connection is established.setDoInput in class URLConnectionnewValue - the new value for the flag to be set.URLConnection.doInputpublic void setDoOutput(boolean newValue)
URLConnectionURLConnection allows
output. It cannot be set after the connection is established.setDoOutput in class URLConnectionnewValue - the new value for the flag to be set.URLConnection.doOutputpublic void setIfModifiedSince(long newValue)
URLConnection0.setIfModifiedSince in class URLConnectionnewValue - the time in milliseconds since January 1, 1970 GMT.URLConnection.ifModifiedSincepublic void setRequestProperty(String field, String newValue)
URLConnectionURLConnection instance. This method can
only be called before the connection is established.setRequestProperty in class URLConnectionfield - the request header field to be set.newValue - the new value of the specified property.public void setUseCaches(boolean newValue)
URLConnectionsetUseCaches in class URLConnectionnewValue - the value of the flag to be set.URLConnection.useCachespublic void setConnectTimeout(int timeoutMillis)
URLConnectionSocketTimeoutException if
the timeout elapses before a connection is established. The default value
of 0 causes us to do a blocking connect. This does not mean we
will never time out, but it probably means you'll get a TCP timeout
after several minutes.
Warning: if the hostname resolves to multiple IP addresses, this client will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses.
setConnectTimeout in class URLConnectionpublic int getConnectTimeout()
URLConnectiongetConnectTimeout in class URLConnectionpublic void setReadTimeout(int timeoutMillis)
URLConnectionSocketTimeoutException if the
timeout elapses before data becomes available. The default value of
0 disables read timeouts; read attempts will block indefinitely.setReadTimeout in class URLConnectiontimeoutMillis - the read timeout in milliseconds. Non-negative.public int getReadTimeout()
URLConnection0 if reads never
timeout.getReadTimeout in class URLConnectionpublic String toString()
URLConnectiontoString in class URLConnectionURLConnection instance.public void setFixedLengthStreamingMode(int contentLength)
HttpURLConnectionsetFixedLengthStreamingMode((long) contentLength),
but available on earlier versions of Android and limited to 2 GiB.setFixedLengthStreamingMode in class HttpURLConnectionpublic void setChunkedStreamingMode(int chunkLength)
HttpURLConnectionWhen HTTP chunked encoding is used, the stream is divided into chunks, each prefixed with a header containing the chunk's size. A large chunk length requires a large internal buffer, potentially wasting memory. A small chunk length increases the number of bytes that must be transmitted because of the header on every chunk.
Implementation details: In some releases the chunkLength is
treated as a hint: chunks sent to the server may actually be larger or
smaller. To force a chunk to be sent to the server call
OutputStream.flush().
setChunkedStreamingMode in class HttpURLConnectionchunkLength - the length to use, or 0 for the default chunk
length.HttpURLConnection.setFixedLengthStreamingMode(long)