public class HttpURLConnectionImpl extends HttpURLConnection
connected field from the superclass. That field
is not used to indicate not whether this URLConnection is
currently connected. Instead, it indicates whether a connection has ever been
attempted. Once a connection has been attempted, certain properties (request
header fields, request method, etc.) are immutable.| Modifier and Type | Field and Description |
|---|---|
protected HttpEngine |
httpEngine |
protected IOException |
httpEngineFailure |
chunkLength, 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 |
|---|
HttpURLConnectionImpl(URL url,
OkHttpClient client) |
| Modifier and Type | Method and Description |
|---|---|
void |
addRequestProperty(String field,
String value)
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.
|
int |
getConnectTimeout()
Returns the connect timeout in milliseconds.
|
InputStream |
getErrorStream()
Returns an input stream from the server in the case of error such as the
requested file (txt, htm, html) is not found on the remote server.
|
String |
getHeaderField(int position)
Returns the value of the field at
position. |
String |
getHeaderField(String fieldName)
Returns the value of the field corresponding to the
fieldName, or
null if there is no such field. |
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.
|
InputStream |
getInputStream()
Returns an
InputStream for reading data from the resource pointed by
this URLConnection. |
OutputStream |
getOutputStream()
Returns an
OutputStream for writing data to this URLConnection. |
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. |
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.
|
void |
setConnectTimeout(int timeoutMillis)
Sets the maximum time in milliseconds to wait while connecting.
|
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 |
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.
|
boolean |
usingProxy()
Returns true if either:
A specific proxy was explicitly configured for this connection.
|
getContentEncoding, getFollowRedirects, getHeaderFieldDate, getInstanceFollowRedirects, getRequestMethod, setChunkedStreamingMode, setFollowRedirectsgetAllowUserInteraction, getContent, getContent, getContentLength, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderFieldInt, getIfModifiedSince, getLastModified, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setUseCaches, toStringprotected IOException httpEngineFailure
protected HttpEngine httpEngine
public HttpURLConnectionImpl(URL url, OkHttpClient client)
public final void connect()
throws IOException
URLConnectionconnect in class URLConnectionIOException - if an error occurs while connecting to the resource.public final 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 final InputStream getErrorStream()
getErrorStream in class HttpURLConnectionpublic final String getHeaderField(int position)
position. Returns null if there
are fewer than position headers.getHeaderField in class URLConnectionposition - the field position of the response header.pos.public final String getHeaderField(String fieldName)
fieldName, or
null if there is no such field. If the field has multiple values, the
last value is returned.getHeaderField in class URLConnectionfieldName - the name of the header field.public final 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 final 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 final Map<String,List<String>> getRequestProperties()
URLConnectiongetRequestProperties in class URLConnectionpublic final 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 final 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 final 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 final 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 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 void setInstanceFollowRedirects(boolean followRedirects)
HttpURLConnectionsetInstanceFollowRedirects in class HttpURLConnectionfollowRedirects - true if this connection will follows redirects, false
otherwise.public 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 final boolean usingProxy()
selected in order to get it.
Warning: This method may return false before attempting to connect and true afterwards.
usingProxy in class HttpURLConnectiontrue if this connection passes a proxy server, false
otherwise.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 final int getResponseCode()
throws IOException
HttpURLConnectiongetResponseCode in class HttpURLConnectionIOException - if there is an IO error during the retrieval.HttpURLConnection.getResponseMessage()public final 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 setIfModifiedSince(long newValue)
URLConnection0.setIfModifiedSince in class URLConnectionnewValue - the time in milliseconds since January 1, 1970 GMT.URLConnection.ifModifiedSincepublic final void addRequestProperty(String field, String value)
URLConnectionaddRequestProperty in class URLConnectionfield - the request property field name to add.value - the value of the property which is to add.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 void setFixedLengthStreamingMode(int contentLength)
HttpURLConnectionsetFixedLengthStreamingMode((long) contentLength),
but available on earlier versions of Android and limited to 2 GiB.setFixedLengthStreamingMode in class HttpURLConnectionpublic void setFixedLengthStreamingMode(long contentLength)
HttpURLConnectioncontentLength.setFixedLengthStreamingMode in class HttpURLConnectioncontentLength - the fixed length of the HTTP request body.HttpURLConnection.setChunkedStreamingMode(int)