public class

HttpURLConnection

extends HttpURLConnection
java.lang.Object
   ↳ java.net.URLConnection
     ↳ java.net.HttpURLConnection
       ↳ sun.net.www.protocol.http.HttpURLConnection
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

A class to represent an HTTP connection to a remote object.

Summary

[Expand]
Inherited Constants
From class java.net.HttpURLConnection
Fields
protected CacheResponse cachedResponse
protected Handler handler
protected HttpClient http
protected Proxy instProxy
protected ProgressSource pi
protected PrintStream ps
public static final String userAgent
[Expand]
Inherited Fields
From class java.net.HttpURLConnection
From class java.net.URLConnection
Public Constructors
HttpURLConnection(URL u, String host, int port)
HttpURLConnection(URL u, Proxy p)
this constructor is used by other protocol handlers such as ftp that want to use http to fetch urls on their behalf.
Protected Constructors
HttpURLConnection(URL u, Handler handler)
HttpURLConnection(URL u, Proxy p, Handler handler)
Public Methods
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)
synchronized void doTunneling()
establish a tunnel through proxy server
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(String name)
Gets a header field by name.
String getHeaderField(int n)
Gets a header field by index.
String getHeaderFieldKey(int n)
Gets a header field by index.
Map getHeaderFields()
Returns an unmodifiable Map of the header fields.
synchronized InputStream getInputStream()
Returns an input stream that reads from this open connection.
synchronized OutputStream getOutputStream()
Returns an output stream that writes to this connection.
int getReadTimeout()
Returns setting for read timeout.
synchronized Map 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.
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.
static void setDefaultAuthenticator(HttpAuthenticator a)
void setReadTimeout(int timeout)
Sets the read timeout to a specified timeout, in milliseconds.
void setRequestProperty(String key, String value)
Sets request property.
boolean usingProxy()
Indicates if the connection is going through a proxy.
Protected Methods
void finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout)
HttpClient getNewHttpClient(URL url, Proxy p, int connectTimeout, boolean useCache)
void plainConnect()
void proxiedConnect(URL url, String proxyHost, int proxyPort, boolean useCache)
void setNewClient(URL url, boolean useCache)
Obtain a HttpsClient object.
void setNewClient(URL url)
Create a new HttpClient object, bypassing the cache of HTTP client objects/connections.
void setProxiedClient(URL url, String proxyHost, int proxyPort)
Create a new HttpClient object, set up so that it uses per-instance proxying to the given HTTP proxy.
void setProxiedClient(URL url, String proxyHost, int proxyPort, boolean useCache)
Obtain a HttpClient object, set up so that it uses per-instance proxying to the given HTTP proxy.
[Expand]
Inherited Methods
From class java.net.HttpURLConnection
From class java.net.URLConnection
From class java.lang.Object

Fields

protected CacheResponse cachedResponse

protected Handler handler

protected HttpClient http

protected Proxy instProxy

protected ProgressSource pi

protected PrintStream ps

public static final String userAgent

Public Constructors

public HttpURLConnection (URL u, String host, int port)

public HttpURLConnection (URL u, Proxy p)

this constructor is used by other protocol handlers such as ftp that want to use http to fetch urls on their behalf.

Protected Constructors

protected HttpURLConnection (URL u, Handler handler)

Throws
IOException

protected HttpURLConnection (URL u, Proxy p, Handler handler)

Public Methods

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.

Parameters
key the keyword by which the request is known (e.g., "accept").
value the value associated with it.
See Also
  • #getRequestProperties(java.lang.String)

public void connect ()

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.

Throws
IOException

public void disconnect ()

Disconnect from the server (public API)

public synchronized void doTunneling ()

establish a tunnel through proxy server

Throws
IOException

public int getConnectTimeout ()

Returns setting for connect timeout.

0 return implies that the option is disabled (i.e., timeout of infinity).

Returns
  • an int that indicates the connect timeout value in milliseconds

public InputStream getErrorStream ()

Returns 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.

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.

public String getHeaderField (String name)

Gets a header field by name. Returns null if not known.

Parameters
name the name of the header field
Returns
  • the value of the named header field, or null if there is no such field in the header.

public String getHeaderField (int n)

Gets a header field by index. Returns null if not known.

Parameters
n the index of the header field
Returns
  • the value of the nth header field, or null if the value does not exist.

public String getHeaderFieldKey (int n)

Gets a header field by index. Returns null if not known.

Parameters
n the index of the header field
Returns
  • the key for the nth header field, or null if the key does not exist.

public Map 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.

Returns
  • a Map of header fields

public synchronized InputStream getInputStream ()

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.

Returns
  • an input stream that reads from this open connection.
Throws
IOException

public synchronized OutputStream getOutputStream ()

Returns an output stream that writes to this connection.

Returns
  • an output stream that writes to this connection.
Throws
IOException

public int getReadTimeout ()

Returns setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).

Returns
  • an int that indicates the read timeout value in milliseconds

public synchronized Map 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.

Returns
  • a Map of the general request properties for this connection.
Throws
IllegalStateException if already connected

public String getRequestProperty (String key)

Returns the value of the named general request property for this connection.

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.

public static InputStream openConnectionCheckRedirects (URLConnection c)

opens a stream allowing redirects only to the same host.

Throws
IOException

public 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. 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().

Parameters
timeout an int that specifies the connect timeout value in milliseconds

public static void setDefaultAuthenticator (HttpAuthenticator a)

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().

Parameters
timeout an int that specifies the timeout value to be used in milliseconds
Throws
IllegalArgumentException if the timeout parameter is negative
See Also
  • java.net.URLConnectiongetReadTimeout()
  • read()

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.

Parameters
key the keyword by which the request is known (e.g., "accept").
value the value to be set

public boolean usingProxy ()

Indicates if the connection is going through a proxy.

Returns
  • a boolean indicating if the connection is using a proxy.

Protected Methods

protected void finalize ()

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.

protected HttpClient getNewHttpClient (URL url, Proxy p, int connectTimeout)

Throws
IOException

protected HttpClient getNewHttpClient (URL url, Proxy p, int connectTimeout, boolean useCache)

Throws
IOException

protected void plainConnect ()

Throws
IOException

protected void proxiedConnect (URL url, String proxyHost, int proxyPort, boolean useCache)

Throws
IOException

protected void setNewClient (URL url, boolean useCache)

Obtain a HttpsClient object. Use the cached copy if specified.

Parameters
url the URL being accessed
useCache whether the cached connection should be used if present
Throws
IOException

protected void setNewClient (URL url)

Create a new HttpClient object, bypassing the cache of HTTP client objects/connections.

Parameters
url the URL being accessed
Throws
IOException

protected void setProxiedClient (URL url, String proxyHost, int proxyPort)

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.

Parameters
url the URL being accessed
proxyHost the proxy host to use
proxyPort the proxy port to use
Throws
IOException

protected void setProxiedClient (URL url, String proxyHost, int proxyPort, boolean useCache)

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.

Parameters
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
Throws
IOException