Module java.base
Package java.net

Class URLConnection

java.lang.Object
java.net.URLConnection
Direct Known Subclasses:
HttpURLConnection,JarURLConnection

public abstract classURLConnectionextendsObject
The abstract classURLConnection is the superclass of all classes that represent a communications link between the application and a URL. Instances of this class can be used both to read from and to write to the resource referenced by the URL.

In general, creating a connection to a URL is a multistep process:

  1. The connection object is created by invoking theopenConnection method on a URL.
  2. The setup parameters and general request properties are manipulated.
  3. The actual connection to the remote object is made, using theconnect method.
  4. The remote object becomes available. The header fields and the contents of the remote object can be accessed.

The setup parameters are modified using the following methods:

  • setAllowUserInteraction
  • setDoInput
  • setDoOutput
  • setIfModifiedSince
  • setUseCaches

and the general request properties are modified using the method:

  • setRequestProperty

Default values for theAllowUserInteraction andUseCaches parameters can be set using the methodssetDefaultAllowUserInteraction andsetDefaultUseCaches.

Each of the aboveset methods has a correspondingget method to retrieve the value of the parameter or general request property. The specific parameters and general request properties that are applicable are protocol specific.

The following methods are used to access the header fields and the contents after the connection is made to the remote object:

  • getContent
  • getHeaderField
  • getInputStream
  • getOutputStream

Certain header fields are accessed frequently. The methods:

  • getContentEncoding
  • getContentLength
  • getContentType
  • getDate
  • getExpiration
  • getLastModified

provide convenient access to these fields. ThegetContentType method is used by thegetContent method to determine the type of the remote object; subclasses may find it convenient to override thegetContentType method.

In the common case, all of the pre-connection parameters and general request properties can be ignored: the pre-connection parameters and request properties default to sensible values. For most clients of this interface, there are only two interesting methods:getInputStream andgetContent, which are mirrored in theURL class by convenience methods.

More information on the request properties and header fields of anhttp connection can be found at:

http://www.ietf.org/rfc/rfc2616.txt
Invoking theclose() methods on theInputStream orOutputStream of anURLConnection after a request may free network resources associated with this instance, unless particular protocol specifications specify different behaviours for it.

Since:
1.0
External Specifications
See Also: