public class HttpURLConnection extends HttpURLConnection
Modifier and Type | Field and Description |
---|---|
protected Handler |
handler |
protected HttpClient |
http |
protected PrintStream |
ps |
static String |
userAgent |
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_UNAUTHORIZED, HTTP_UNAVAILABLE, HTTP_UNSUPPORTED_TYPE, HTTP_USE_PROXY, HTTP_VERSION, instanceFollowRedirects, method, responseCode, responseMessage
allowUserInteraction, connected, doInput, doOutput, url, useCaches
Modifier | Constructor and Description |
---|---|
|
HttpURLConnection(URL u)
this constructor is used by other protocol handlers such as ftp that want to use http to fetch urls on their
behalf.
|
protected |
HttpURLConnection(URL u,
Handler handler) |
Modifier and Type | Method and Description |
---|---|
void |
addRequestProperty(String key,
String value)
Adds a general request property specified by a key-value pair.
|
void |
connect()
Opens a communications link to the resource referenced by this URL, if such a connection has not already been
established.
|
void |
disconnect()
Disconnect from the server (public API)
|
int |
getConnectTimeout()
Returns setting for connect timeout.
|
InputStream |
getErrorStream()
Returns the error stream if the connection failed but the server sent useful data nonetheless.
|
String |
getHeaderField(int n)
Gets a header field by index.
|
String |
getHeaderField(String name)
Gets a header field by name.
|
String |
getHeaderFieldKey(int n)
Gets a header field by index.
|
Map<String,List<String>> |
getHeaderFields()
Returns an unmodifiable Map of the header fields.
|
InputStream |
getInputStream()
Returns an input stream that reads from this open connection.
|
protected HttpClient |
getNewHttpClient(URL url,
int connectTimeout) |
OutputStream |
getOutputStream()
Returns an output stream that writes to this connection.
|
int |
getReadTimeout()
Returns setting for read timeout.
|
Map<String,List<String>> |
getRequestProperties()
Returns an unmodifiable Map of general request properties for this connection.
|
String |
getRequestProperty(String key)
Returns the value of the named general request property for this connection.
|
static InputStream |
openConnectionCheckRedirects(URLConnection c)
opens a stream allowing redirects only to the same host.
|
protected void |
plainConnect() |
void |
setAuthenticationProperty(String key,
String value) |
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.
|
protected void |
setNewClient(URL url)
Obtain a HttpsClient object.
|
void |
setReadTimeout(int timeout)
Sets the read timeout to a specified timeout, in milliseconds.
|
void |
setRequestProperty(String key,
String value)
Sets request property.
|
boolean |
streaming() |
boolean |
usingProxy()
Indicates if the connection is going through a proxy.
|
getFollowRedirects, getInstanceFollowRedirects, getPermission, getRequestMethod, getResponseCode, getResponseMessage, setChunkedStreamingMode, setFixedLengthStreamingMode, setFixedLengthStreamingMode, setFollowRedirects, setInstanceFollowRedirects, setRequestMethod
getAllowUserInteraction, getContent, getContent, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDefaultAllowUserInteraction, getDefaultUseCaches, getDoInput, getDoOutput, getHeaderFieldInt, getHeaderFieldLong, getURL, getUseCaches, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultUseCaches, setDoInput, setDoOutput, setUseCaches, toString
protected Handler handler
@Nullable protected HttpClient http
@Nullable protected PrintStream ps
public static final String userAgent
public HttpURLConnection(URL u)
public void addRequestProperty(String key, String value)
addRequestProperty
in class URLConnection
key
- the keyword by which the request is known (e.g., "accept
").value
- the value associated with it.getRequestProperties()
public void connect() throws IOException
URLConnection
If the connect
method is called when the connection has already been opened (indicated by the
connected
field having the value true
), the call is ignored.
URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.
connect
in class URLConnection
IOException
- if an I/O error occurs while opening the connection.URLConnection.connected
,
URLConnection.getConnectTimeout()
,
URLConnection.setConnectTimeout(int)
public void disconnect()
disconnect
in class HttpURLConnection
public int getConnectTimeout()
0 return implies that the option is disabled (i.e., timeout of infinity).
getConnectTimeout
in class URLConnection
int
that indicates the connect timeout value in millisecondsURLConnection.setConnectTimeout(int)
,
URLConnection.connect()
@Nullable public InputStream getErrorStream()
HttpURLConnection
This method will not cause a connection to be initiated. If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, this method will return null. This is the default.
getErrorStream
in class HttpURLConnection
@Nullable public String getHeaderField(int n)
getHeaderField
in class HttpURLConnection
n
- the index of the header fieldn
th header field, or null
if the value does not exist.HttpURLConnection.getHeaderFieldKey(int)
@Nullable public String getHeaderField(String name)
getHeaderField
in class URLConnection
name
- the name of the header fieldnull
if there is no such field in the header.@Nullable public String getHeaderFieldKey(int n)
getHeaderFieldKey
in class HttpURLConnection
n
- the index of the header fieldn
th header field, or null
if the key does not exist.public Map<String,List<String>> getHeaderFields()
getHeaderFields
in class URLConnection
public InputStream getInputStream() throws IOException
URLConnection
getInputStream
in class URLConnection
IOException
- if an I/O error occurs while creating the input stream.UnknownServiceException
- if the protocol does not support input.URLConnection.setReadTimeout(int)
,
URLConnection.getReadTimeout()
protected HttpClient getNewHttpClient(URL url, int connectTimeout) throws IOException
IOException
public OutputStream getOutputStream() throws IOException
URLConnection
getOutputStream
in class URLConnection
IOException
- if an I/O error occurs while creating the output stream.UnknownServiceException
- if the protocol does not support output.public int getReadTimeout()
getReadTimeout
in class URLConnection
int
that indicates the read timeout value in millisecondsURLConnection.setReadTimeout(int)
,
InputStream.read()
public Map<String,List<String>> getRequestProperties()
getRequestProperties
in class URLConnection
IllegalStateException
- if already connected@Nullable public String getRequestProperty(@Nullable String key)
URLConnection
getRequestProperty
in class URLConnection
key
- the keyword by which the request is known (e.g., "Accept").URLConnection.setRequestProperty(java.lang.String, java.lang.String)
public static InputStream openConnectionCheckRedirects(URLConnection c) throws IOException
IOException
protected void plainConnect() throws IOException
IOException
public void setConnectTimeout(int timeout)
URLConnection
Some non-standard implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().
setConnectTimeout
in class URLConnection
timeout
- an int
that specifies the connect timeout value in millisecondsURLConnection.getConnectTimeout()
,
URLConnection.connect()
protected void setNewClient(URL url) throws IOException
url
- the URL being accessedIOException
public void setReadTimeout(int timeout)
Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().
setReadTimeout
in class URLConnection
timeout
- an int
that specifies the timeout value to be used in millisecondsIllegalArgumentException
- if the timeout parameter is negativeInputStream.read()
public void setRequestProperty(String key, String value)
setRequestProperty
in class URLConnection
value
- the value to be setkey
- the keyword by which the request is known (e.g., "Accept
").URLConnection.getRequestProperty(java.lang.String)
public boolean streaming()
public boolean usingProxy()
HttpURLConnection
usingProxy
in class HttpURLConnection