java.lang.Object | |||
↳ | java.net.URLConnection | ||
↳ | java.net.HttpURLConnection | ||
↳ | sun.net.www.protocol.http.HttpURLConnection |
![]() |
![]() |
A class to represent an HTTP connection to a remote object.
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
cachedResponse | |||||||||||
handler | |||||||||||
http | |||||||||||
instProxy | |||||||||||
pi | |||||||||||
ps | |||||||||||
userAgent |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
this constructor is used by other protocol handlers such as ftp
that want to use http to fetch urls on their behalf.
|
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds a general request property specified by a
key-value pair.
| |||||||||||
Opens a communications link to the resource referenced by this
URL, if such a connection has not already been established.
| |||||||||||
Disconnect from the server (public API)
| |||||||||||
establish a tunnel through proxy server
| |||||||||||
Returns setting for connect timeout.
| |||||||||||
Returns the error stream if the connection failed
but the server sent useful data nonetheless.
| |||||||||||
Gets a header field by name.
| |||||||||||
Gets a header field by index.
| |||||||||||
Gets a header field by index.
| |||||||||||
Returns an unmodifiable Map of the header fields.
| |||||||||||
Returns an input stream that reads from this open connection.
| |||||||||||
Returns an output stream that writes to this connection.
| |||||||||||
Returns setting for read timeout.
| |||||||||||
Returns an unmodifiable Map of general request
properties for this connection.
| |||||||||||
Returns the value of the named general request property for this
connection.
| |||||||||||
opens a stream allowing redirects only to the same host.
| |||||||||||
Sets a specified timeout value, in milliseconds, to be used
when opening a communications link to the resource referenced
by this URLConnection.
| |||||||||||
Sets the read timeout to a specified timeout, in
milliseconds.
| |||||||||||
Sets request property.
| |||||||||||
Indicates if the connection is going through a proxy.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
| |||||||||||
Obtain a HttpsClient object.
| |||||||||||
Create a new HttpClient object, bypassing the cache of
HTTP client objects/connections.
| |||||||||||
Create a new HttpClient object, set up so that it uses
per-instance proxying to the given HTTP proxy.
| |||||||||||
Obtain a HttpClient object, set up so that it uses per-instance
proxying to the given HTTP proxy.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() | |||||||||||
![]() |
this constructor is used by other protocol handlers such as ftp that want to use http to fetch urls on their behalf.
Adds a general request property specified by a key-value pair. This method will not overwrite existing values associated with the same key.
key | the keyword by which the request is known
(e.g., "accept "). |
---|---|
value | the value associated with it. |
Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.
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.
IOException |
---|
Disconnect from the server (public API)
Returns setting for connect timeout.
0 return implies that the option is disabled (i.e., timeout of infinity).
int
that indicates the connect timeout
value in millisecondsReturns 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.
Gets a header field by name. Returns null if not known.
name | the name of the header field |
---|
null
if there is no such field in the header.
Gets a header field by index. Returns null if not known.
n | the index of the header field |
---|
n
th header field,
or null
if the value does not exist.Gets a header field by index. Returns null if not known.
n | the index of the header field |
---|
n
th header field,
or null
if the key does not exist.
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.
Returns 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.
IOException |
---|
Returns an output stream that writes to this connection.
IOException |
---|
Returns setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).
int
that indicates the read timeout
value in millisecondsReturns 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.
IllegalStateException | if already connected |
---|
Returns the value of the named general request property for this connection.
key | the keyword by which the request is known (e.g., "accept"). |
---|
opens a stream allowing redirects only to the same host.
IOException |
---|
Sets 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 implmentation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().
timeout | an int that specifies the connect
timeout value in milliseconds |
---|
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().
timeout | an int that specifies the timeout
value to be used in milliseconds |
---|
IllegalArgumentException | if the timeout parameter is negative |
---|
read()
Sets request property. If a property with the key already exists, overwrite its value with the new value.
key | the keyword by which the request is known
(e.g., "accept "). |
---|---|
value | the value to be set |
Indicates if the connection is going through a proxy.
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
A subclass overrides the finalize
method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.
The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
The finalize method is never invoked more than once by a Java virtual machine for any given object.
Any exception thrown by the finalize
method causes
the finalization of this object to be halted, but is otherwise
ignored.
IOException |
---|
IOException |
---|
Obtain a HttpsClient object. Use the cached copy if specified.
url | the URL being accessed |
---|---|
useCache | whether the cached connection should be used if present |
IOException |
---|
Create a new HttpClient object, bypassing the cache of HTTP client objects/connections.
url | the URL being accessed |
---|
IOException |
---|
Create a new HttpClient object, set up so that it uses per-instance proxying to the given HTTP proxy. This bypasses the cache of HTTP client objects/connections.
url | the URL being accessed |
---|---|
proxyHost | the proxy host to use |
proxyPort | the proxy port to use |
IOException |
---|
Obtain a HttpClient object, set up so that it uses per-instance proxying to the given HTTP proxy. Use the cached copy of HTTP client objects/connections if specified.
url | the URL being accessed |
---|---|
proxyHost | the proxy host to use |
proxyPort | the proxy port to use |
useCache | whether the cached connection should be used if present |
IOException |
---|