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, responseMessageallowUserInteraction, 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, setRequestMethodgetAllowUserInteraction, getContent, getContent, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDefaultAllowUserInteraction, getDefaultUseCaches, getDoInput, getDoOutput, getHeaderFieldInt, getHeaderFieldLong, getURL, getUseCaches, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultUseCaches, setDoInput, setDoOutput, setUseCaches, toStringprotected 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 URLConnectionkey - 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 URLConnectionIOException - if an I/O error occurs while opening the connection.URLConnection.connected,
URLConnection.getConnectTimeout(),
URLConnection.setConnectTimeout(int)public void disconnect()
disconnect in class HttpURLConnectionpublic int getConnectTimeout()
0 return implies that the option is disabled (i.e., timeout of infinity).
getConnectTimeout in class URLConnectionint that indicates the connect timeout value in millisecondsURLConnection.setConnectTimeout(int),
URLConnection.connect()@Nullable public InputStream getErrorStream()
HttpURLConnectionThis 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 HttpURLConnectionn - the index of the header fieldnth header field, or null if the value does not exist.HttpURLConnection.getHeaderFieldKey(int)@Nullable public String getHeaderField(String name)
getHeaderField in class URLConnectionname - the name of the header fieldnull if there is no such field in the header.@Nullable public String getHeaderFieldKey(int n)
getHeaderFieldKey in class HttpURLConnectionn - the index of the header fieldnth header field, or null if the key does not exist.public Map<String,List<String>> getHeaderFields()
getHeaderFields in class URLConnectionpublic InputStream getInputStream() throws IOException
URLConnectiongetInputStream in class URLConnectionIOException - 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
IOExceptionpublic OutputStream getOutputStream() throws IOException
URLConnectiongetOutputStream in class URLConnectionIOException - 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 URLConnectionint that indicates the read timeout value in millisecondsURLConnection.setReadTimeout(int),
InputStream.read()public Map<String,List<String>> getRequestProperties()
getRequestProperties in class URLConnectionIllegalStateException - if already connected@Nullable public String getRequestProperty(@Nullable String key)
URLConnectiongetRequestProperty in class URLConnectionkey - 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
IOExceptionprotected void plainConnect()
throws IOException
IOExceptionpublic void setConnectTimeout(int timeout)
URLConnectionSome non-standard implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().
setConnectTimeout in class URLConnectiontimeout - an int that specifies the connect timeout value in millisecondsURLConnection.getConnectTimeout(),
URLConnection.connect()protected void setNewClient(URL url) throws IOException
url - the URL being accessedIOExceptionpublic 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 URLConnectiontimeout - 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 URLConnectionvalue - 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()
HttpURLConnectionusingProxy in class HttpURLConnection