Class HttpURLConnection
- java.lang.Object
-
- java.net.URLConnection
-
- java.net.HttpURLConnection
-
- sun.net.www.protocol.http.HttpURLConnection
-
- Direct Known Subclasses:
AbstractDelegateHttpsURLConnection
public class HttpURLConnection extends HttpURLConnection
A class to represent an HTTP connection to a remote object.
-
-
Field Summary
Fields Modifier and Type Field Description protected Handlerhandlerprotected HttpClienthttpprotected PrintStreampsstatic StringuserAgent-
Fields inherited from class java.net.HttpURLConnection
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
-
Fields inherited from class java.net.URLConnection
allowUserInteraction, connected, doInput, doOutput, url, useCaches
-
-
Constructor Summary
Constructors Modifier Constructor 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.protectedHttpURLConnection(URL u, Handler handler)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRequestProperty(String key, String value)Adds a general request property specified by a key-value pair.voidconnect()Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.voiddisconnect()Disconnect from the server (public API)intgetConnectTimeout()Returns setting for connect timeout.InputStreamgetErrorStream()Returns the error stream if the connection failed but the server sent useful data nonetheless.StringgetHeaderField(int n)Gets a header field by index.StringgetHeaderField(String name)Gets a header field by name.StringgetHeaderFieldKey(int n)Gets a header field by index.Map<String,List<String>>getHeaderFields()Returns an unmodifiable Map of the header fields.InputStreamgetInputStream()Returns an input stream that reads from this open connection.protected HttpClientgetNewHttpClient(URL url, int connectTimeout)OutputStreamgetOutputStream()Returns an output stream that writes to this connection.intgetReadTimeout()Returns setting for read timeout.Map<String,List<String>>getRequestProperties()Returns an unmodifiable Map of general request properties for this connection.StringgetRequestProperty(String key)Returns the value of the named general request property for this connection.static InputStreamopenConnectionCheckRedirects(URLConnection c)opens a stream allowing redirects only to the same host.protected voidplainConnect()voidsetAuthenticationProperty(String key, String value)voidsetConnectTimeout(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 voidsetNewClient(URL url)Obtain a HttpsClient object.voidsetReadTimeout(int timeout)Sets the read timeout to a specified timeout, in milliseconds.voidsetRequestProperty(String key, String value)Sets request property.booleanstreaming()booleanusingProxy()Indicates if the connection is going through a proxy.-
Methods inherited from class java.net.HttpURLConnection
getFollowRedirects, getInstanceFollowRedirects, getPermission, getRequestMethod, getResponseCode, getResponseMessage, setChunkedStreamingMode, setFixedLengthStreamingMode, setFixedLengthStreamingMode, setFollowRedirects, setInstanceFollowRedirects, setRequestMethod
-
Methods inherited from class java.net.URLConnection
getAllowUserInteraction, getContent, getContent, getContentEncoding, getContentLength, getContentLengthLong, getContentType, getDefaultAllowUserInteraction, getDefaultUseCaches, getDoInput, getDoOutput, getHeaderFieldInt, getHeaderFieldLong, getURL, getUseCaches, setAllowUserInteraction, setContentHandlerFactory, setDefaultAllowUserInteraction, setDefaultUseCaches, setDoInput, setDoOutput, setUseCaches, toString
-
-
-
-
Field Detail
-
userAgent
public static final String userAgent
-
http
@Nullable protected HttpClient http
-
handler
protected Handler handler
-
ps
@Nullable protected PrintStream ps
-
-
Method Detail
-
setNewClient
protected void setNewClient(URL url) throws IOException
Obtain a HttpsClient object. Use the cached copy if specified.- Parameters:
url- the URL being accessed- Throws:
IOException
-
openConnectionCheckRedirects
public static InputStream openConnectionCheckRedirects(URLConnection c) throws IOException
opens a stream allowing redirects only to the same host.- Throws:
IOException
-
connect
public void connect() throws IOExceptionDescription copied from class:URLConnectionOpens a communications link to the resource referenced by this URL, if such a connection has not already been established.If the
connectmethod is called when the connection has already been opened (indicated by theconnectedfield having the valuetrue), 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.
- Specified by:
connectin classURLConnection- Throws:
IOException- if an I/O error occurs while opening the connection.- See Also:
URLConnection.connected,URLConnection.getConnectTimeout(),URLConnection.setConnectTimeout(int)
-
plainConnect
protected void plainConnect() throws IOException- Throws:
IOException
-
getNewHttpClient
protected HttpClient getNewHttpClient(URL url, int connectTimeout) throws IOException
- Throws:
IOException
-
getOutputStream
public OutputStream getOutputStream() throws IOException
Description copied from class:URLConnectionReturns an output stream that writes to this connection.- Overrides:
getOutputStreamin classURLConnection- Returns:
- an output stream that writes to this connection.
- Throws:
IOException- if an I/O error occurs while creating the output stream.UnknownServiceException- if the protocol does not support output.
-
streaming
public boolean streaming()
-
getInputStream
public InputStream getInputStream() throws IOException
Description copied from class:URLConnectionReturns an input stream that reads from this open connection. A SocketTimeoutException can be thrown when reading from the returned input stream if the read timeout expires before data is available for read.- Overrides:
getInputStreamin classURLConnection- Returns:
- an input stream that reads from this open connection.
- Throws:
IOException- if an I/O error occurs while creating the input stream.UnknownServiceException- if the protocol does not support input.- See Also:
URLConnection.setReadTimeout(int),URLConnection.getReadTimeout()
-
getErrorStream
@Nullable public InputStream getErrorStream()
Description copied from class:HttpURLConnectionReturns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.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.
- Overrides:
getErrorStreamin classHttpURLConnection- Returns:
- an error stream if any, null if there have been no errors, the connection is not connected or the server sent no useful data.
-
disconnect
public void disconnect()
Disconnect from the server (public API)- Specified by:
disconnectin classHttpURLConnection
-
usingProxy
public boolean usingProxy()
Description copied from class:HttpURLConnectionIndicates if the connection is going through a proxy.- Specified by:
usingProxyin classHttpURLConnection- Returns:
- a boolean indicating if the connection is using a proxy.
-
getHeaderField
@Nullable public String getHeaderField(String name)
Gets a header field by name. Returns null if not known.- Overrides:
getHeaderFieldin classURLConnection- Parameters:
name- the name of the header field- Returns:
- the value of the named header field, or
nullif there is no such field in the header.
-
getHeaderFields
public Map<String,List<String>> getHeaderFields()
Returns an unmodifiable Map of the header fields. The Map keys are Strings that represent the response-header field names. Each Map value is an unmodifiable List of Strings that represents the corresponding field values.- Overrides:
getHeaderFieldsin classURLConnection- Returns:
- a Map of header fields
- Since:
- 1.4
-
getHeaderField
@Nullable public String getHeaderField(int n)
Gets a header field by index. Returns null if not known.- Overrides:
getHeaderFieldin classHttpURLConnection- Parameters:
n- the index of the header field- Returns:
- the value of the
nth header field, ornullif the value does not exist. - See Also:
HttpURLConnection.getHeaderFieldKey(int)
-
getHeaderFieldKey
@Nullable public String getHeaderFieldKey(int n)
Gets a header field by index. Returns null if not known.- Overrides:
getHeaderFieldKeyin classHttpURLConnection- Parameters:
n- the index of the header field- Returns:
- the key for the
nth header field, ornullif the key does not exist.
-
setRequestProperty
public void setRequestProperty(String key, String value)
Sets request property. If a property with the key already exists, overwrite its value with the new value.- Overrides:
setRequestPropertyin classURLConnection- Parameters:
value- the value to be setkey- the keyword by which the request is known (e.g., "Accept").- See Also:
URLConnection.getRequestProperty(java.lang.String)
-
addRequestProperty
public void addRequestProperty(String key, String value)
Adds a general request property specified by a key-value pair. This method will not overwrite existing values associated with the same key.- Overrides:
addRequestPropertyin classURLConnection- Parameters:
key- the keyword by which the request is known (e.g., "accept").value- the value associated with it.- Since:
- 1.4
- See Also:
getRequestProperties()
-
getRequestProperty
@Nullable public String getRequestProperty(@Nullable String key)
Description copied from class:URLConnectionReturns the value of the named general request property for this connection.- Overrides:
getRequestPropertyin classURLConnection- Parameters:
key- the keyword by which the request is known (e.g., "Accept").- Returns:
- the value of the named general request property for this connection. If key is null, then null is returned.
- See Also:
URLConnection.setRequestProperty(java.lang.String, java.lang.String)
-
getRequestProperties
public Map<String,List<String>> getRequestProperties()
Returns an unmodifiable Map of general request properties for this connection. The Map keys are Strings that represent the request-header field names. Each Map value is a unmodifiable List of Strings that represents the corresponding field values.- Overrides:
getRequestPropertiesin classURLConnection- Returns:
- a Map of the general request properties for this connection.
- Throws:
IllegalStateException- if already connected- Since:
- 1.4
-
setConnectTimeout
public void setConnectTimeout(int timeout)
Description copied from class:URLConnectionSets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.Some non-standard implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().
- Overrides:
setConnectTimeoutin classURLConnection- Parameters:
timeout- anintthat specifies the connect timeout value in milliseconds- See Also:
URLConnection.getConnectTimeout(),URLConnection.connect()
-
getConnectTimeout
public int getConnectTimeout()
Returns setting for connect timeout.0 return implies that the option is disabled (i.e., timeout of infinity).
- Overrides:
getConnectTimeoutin classURLConnection- Returns:
- an
intthat indicates the connect timeout value in milliseconds - Since:
- 1.5
- See Also:
URLConnection.setConnectTimeout(int),URLConnection.connect()
-
setReadTimeout
public void setReadTimeout(int timeout)
Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().
- Overrides:
setReadTimeoutin classURLConnection- Parameters:
timeout- anintthat specifies the timeout value to be used in milliseconds- Throws:
IllegalArgumentException- if the timeout parameter is negative- Since:
- 1.5
- See Also:
InputStream.read()
-
getReadTimeout
public int getReadTimeout()
Returns setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).- Overrides:
getReadTimeoutin classURLConnection- Returns:
- an
intthat indicates the read timeout value in milliseconds - Since:
- 1.5
- See Also:
URLConnection.setReadTimeout(int),InputStream.read()
-
-