public final class OkHttpURLConnection extends java.net.HttpURLConnection implements Callback
connected field from the
superclass. That field is not used to indicate 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 | Class and Description |
|---|---|
(package private) class |
OkHttpURLConnection.NetworkInterceptor
The HttpURLConnection gives the application control between establishing the connection and
transmitting the request body.
|
(package private) static class |
OkHttpURLConnection.UnexpectedException |
| Modifier and Type | Field and Description |
|---|---|
(package private) Call |
call |
private java.lang.Throwable |
callFailure |
(package private) OkHttpClient |
client |
(package private) boolean |
connectPending |
private boolean |
executed |
private long |
fixedContentLength
Like the superclass field of the same name, but a long and available on all platforms.
|
(package private) Handshake |
handshake |
private java.lang.Object |
lock |
private static java.util.Set<java.lang.String> |
METHODS |
private OkHttpURLConnection.NetworkInterceptor |
networkInterceptor |
(package private) Response |
networkResponse |
(package private) java.net.Proxy |
proxy |
private Headers.Builder |
requestHeaders |
private Response |
response |
static java.lang.String |
RESPONSE_SOURCE
Synthetic response header: the location from which the response was loaded.
|
private Headers |
responseHeaders
Lazily created (with synthetic headers) on first call to getHeaders().
|
static java.lang.String |
SELECTED_PROTOCOL
Synthetic response header: the selected
protocol ("spdy/3.1", "http/1.1",
etc). |
(package private) URLFilter |
urlFilter |
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, responseMessage| Constructor and Description |
|---|
OkHttpURLConnection(java.net.URL url,
OkHttpClient client) |
OkHttpURLConnection(java.net.URL url,
OkHttpClient client,
URLFilter urlFilter) |
| Modifier and Type | Method and Description |
|---|---|
void |
addRequestProperty(java.lang.String field,
java.lang.String value) |
private Call |
buildCall() |
void |
connect() |
private java.lang.String |
defaultUserAgent() |
void |
disconnect() |
int |
getConnectTimeout() |
java.io.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.
|
java.lang.String |
getHeaderField(int position)
Returns the value of the field at
position. |
java.lang.String |
getHeaderField(java.lang.String fieldName)
Returns the value of the field corresponding to the
fieldName, or null if there is no
such field. |
java.lang.String |
getHeaderFieldKey(int position) |
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getHeaderFields() |
private Headers |
getHeaders() |
java.io.InputStream |
getInputStream() |
boolean |
getInstanceFollowRedirects() |
java.io.OutputStream |
getOutputStream() |
java.security.Permission |
getPermission() |
int |
getReadTimeout() |
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getRequestProperties() |
java.lang.String |
getRequestProperty(java.lang.String field) |
private Response |
getResponse(boolean networkResponseOnError)
Aggressively tries to get the final HTTP response, potentially making many HTTP requests in the
process in order to cope with redirects and authentication.
|
int |
getResponseCode() |
java.lang.String |
getResponseMessage() |
void |
onFailure(Call call,
java.io.IOException e)
Called when the request could not be executed due to cancellation, a connectivity problem or
timeout.
|
void |
onResponse(Call call,
Response response)
Called when the HTTP response was successfully returned by the remote server.
|
private static java.io.IOException |
propagate(java.lang.Throwable throwable)
Throws
throwable as either an IOException, RuntimeException, or Error. |
private static java.lang.String |
responseSourceHeader(Response response) |
void |
setConnectTimeout(int timeoutMillis) |
void |
setFixedLengthStreamingMode(int contentLength) |
void |
setFixedLengthStreamingMode(long contentLength) |
void |
setIfModifiedSince(long newValue) |
void |
setInstanceFollowRedirects(boolean followRedirects) |
void |
setReadTimeout(int timeoutMillis) |
void |
setRequestMethod(java.lang.String method) |
void |
setRequestProperty(java.lang.String field,
java.lang.String newValue) |
private static java.lang.String |
toHumanReadableAscii(java.lang.String s)
Returns
s with control characters and non-ASCII characters replaced with '?'. |
boolean |
usingProxy()
Returns true if either:
A specific proxy was explicitly configured for this connection.
|
getFollowRedirects, getHeaderFieldDate, getRequestMethod, setChunkedStreamingMode, setFollowRedirectsgetAllowUserInteraction, getContent, getContent, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDate, getDefaultAllowUserInteraction, getDefaultRequestProperty, getDefaultUseCaches, getDoInput, getDoOutput, getExpiration, getFileNameMap, getHeaderFieldInt, getHeaderFieldLong, getIfModifiedSince, getLastModified, getURL, getUseCaches, guessContentTypeFromName, guessContentTypeFromStream, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultRequestProperty, setDefaultUseCaches, setDoInput, setDoOutput, setFileNameMap, setUseCaches, toStringpublic static final java.lang.String SELECTED_PROTOCOL
protocol ("spdy/3.1", "http/1.1",
etc).public static final java.lang.String RESPONSE_SOURCE
private static final java.util.Set<java.lang.String> METHODS
OkHttpClient client
private final OkHttpURLConnection.NetworkInterceptor networkInterceptor
private Headers.Builder requestHeaders
private boolean executed
Call call
URLFilter urlFilter
private Headers responseHeaders
private long fixedContentLength
private final java.lang.Object lock
private Response response
private java.lang.Throwable callFailure
Response networkResponse
boolean connectPending
java.net.Proxy proxy
Handshake handshake
public OkHttpURLConnection(java.net.URL url,
OkHttpClient client)
public OkHttpURLConnection(java.net.URL url,
OkHttpClient client,
URLFilter urlFilter)
public void connect()
throws java.io.IOException
connect in class java.net.URLConnectionjava.io.IOExceptionpublic void disconnect()
disconnect in class java.net.HttpURLConnectionpublic java.io.InputStream getErrorStream()
getErrorStream in class java.net.HttpURLConnectionprivate Headers getHeaders() throws java.io.IOException
java.io.IOExceptionprivate static java.lang.String responseSourceHeader(Response response)
public java.lang.String getHeaderField(int position)
position. Returns null if there are fewer than position headers.getHeaderField in class java.net.HttpURLConnectionpublic java.lang.String getHeaderField(java.lang.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 java.net.URLConnectionpublic java.lang.String getHeaderFieldKey(int position)
getHeaderFieldKey in class java.net.HttpURLConnectionpublic java.util.Map<java.lang.String,java.util.List<java.lang.String>> getHeaderFields()
getHeaderFields in class java.net.URLConnectionpublic java.util.Map<java.lang.String,java.util.List<java.lang.String>> getRequestProperties()
getRequestProperties in class java.net.URLConnectionpublic java.io.InputStream getInputStream()
throws java.io.IOException
getInputStream in class java.net.URLConnectionjava.io.IOExceptionpublic java.io.OutputStream getOutputStream()
throws java.io.IOException
getOutputStream in class java.net.URLConnectionjava.io.IOExceptionpublic java.security.Permission getPermission()
throws java.io.IOException
getPermission in class java.net.HttpURLConnectionjava.io.IOExceptionpublic java.lang.String getRequestProperty(java.lang.String field)
getRequestProperty in class java.net.URLConnectionpublic void setConnectTimeout(int timeoutMillis)
setConnectTimeout in class java.net.URLConnectionpublic void setInstanceFollowRedirects(boolean followRedirects)
setInstanceFollowRedirects in class java.net.HttpURLConnectionpublic boolean getInstanceFollowRedirects()
getInstanceFollowRedirects in class java.net.HttpURLConnectionpublic int getConnectTimeout()
getConnectTimeout in class java.net.URLConnectionpublic void setReadTimeout(int timeoutMillis)
setReadTimeout in class java.net.URLConnectionpublic int getReadTimeout()
getReadTimeout in class java.net.URLConnectionprivate Call buildCall() throws java.io.IOException
java.io.IOExceptionprivate java.lang.String defaultUserAgent()
private static java.lang.String toHumanReadableAscii(java.lang.String s)
s with control characters and non-ASCII characters replaced with '?'.private Response getResponse(boolean networkResponseOnError) throws java.io.IOException
java.io.IOExceptionpublic boolean usingProxy()
selected in order to get it.
Warning: This method may return false before attempting to connect and true afterwards.
usingProxy in class java.net.HttpURLConnectionpublic java.lang.String getResponseMessage()
throws java.io.IOException
getResponseMessage in class java.net.HttpURLConnectionjava.io.IOExceptionpublic int getResponseCode()
throws java.io.IOException
getResponseCode in class java.net.HttpURLConnectionjava.io.IOExceptionpublic void setRequestProperty(java.lang.String field,
java.lang.String newValue)
setRequestProperty in class java.net.URLConnectionpublic void setIfModifiedSince(long newValue)
setIfModifiedSince in class java.net.URLConnectionpublic void addRequestProperty(java.lang.String field,
java.lang.String value)
addRequestProperty in class java.net.URLConnectionpublic void setRequestMethod(java.lang.String method)
throws java.net.ProtocolException
setRequestMethod in class java.net.HttpURLConnectionjava.net.ProtocolExceptionpublic void setFixedLengthStreamingMode(int contentLength)
setFixedLengthStreamingMode in class java.net.HttpURLConnectionpublic void setFixedLengthStreamingMode(long contentLength)
setFixedLengthStreamingMode in class java.net.HttpURLConnectionpublic void onFailure(Call call, java.io.IOException e)
Callbackpublic void onResponse(Call call, Response response)
CallbackResponse.body. The response is still live until
its response body is closed. The recipient of the callback may
consume the response body on another thread.
Note that transport-layer success (receiving a HTTP response code, headers and body) does
not necessarily indicate application-layer success: response may still indicate an
unhappy HTTP response code like 404 or 500.
onResponse in interface Callbackprivate static java.io.IOException propagate(java.lang.Throwable throwable)
throws java.io.IOException
throwable as either an IOException, RuntimeException, or Error.java.io.IOException