All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----java.net.URLConnection
URLConnection 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:
openConnection() |
connect() |
|---|---|
| Manipulate parameters that affect the connection to the remote resource. | Interact with the resource; query header fields and contents. |
openConnection method on a URL.
connect method.
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 the AllowUserInteraction and
UseCaches parameters can be set using the methods
setDefaultAllowUserInteraction and
setDefaultUseCaches. Default values for general
request properties can be set using the
setDefaultRequestProperty method.
Each of the above set methods has a corresponding
get 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
- getLastModifed
provide convenient access to these fields. The
getContentType method is used by the
getContent method to determine the type of the remote
object; subclasses may find it convenient to override the
getContentType 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 and getObject,
which are mirrored in the URL class by convenience methods.
More information on the request properties and header fields of
an http connection can be found at:
http://www.w3.org/hypertext/WWW/Protocols/HTTP1.0/draft-ietf-http-spec.html
true, this URL is being examined in
a context in which it makes sense to allow user interactions such
as popping up an authentication dialog.
false, this connection object has not created a
communications link to the specified URL.
setDoInput method.
setDoOutput method.
true, the protocol is allowed to use caching
whenever it can.
allowUserInteraction field for
this object.
content-encoding header field.
content-length header field.
content-type header field.
date header field.
allowUserInteraction
field.
URLConnection's
useCaches flag.
URLConnection's
doInput flag.
URLConnection's
doOutput flag.
expires header field.
nth header field.
nth header field.
ifModifiedSince field.
last-modified header field.
URLConnection's URL
field.
URLConnection's
useCaches field.
allowUserInteraction field of
this URLConnection.
ContentHandlerFactory of an
application.
allowUserInteraction field for all future
URLConnection objects to the specified value.
useCaches field to the
specified value.
doInput field for this
URLConnection to the specified value.
doOutput field for this
URLConnection to the specified value.
ifModifiedSince field of
this URLConnection to the specified value.
useCaches field of this
URLConnection to the specified value.
String representation of this URL connection.
Properties Inheritable
url=URL
The value of this field can be accessed by the
getURL method.
The default value of this variable is the value of the URL
argument in the URLConnection constructor.
Properties Inheritable doInput=boolean
setDoInput method. Its
value is returned by the getDoInput method.
A URL connection can be used for input and/or output. Setting the
doInput flag to true indicates that
the application intends to read data from the URL connection.
The default value of this field is true.
Properties Inheritable doOutput=boolean
setDoOutput method. Its
value is returned by the getDoInput method.
A URL connection can be used for input and/or output. Setting the
doOutput flag to true indicates
that the application intends to write data to the URL connection.
The default value of this field is false.
Properties Inheritable allowUserInteraction=boolean
true, this URL is being examined in
a context in which it makes sense to allow user interactions such
as popping up an authentication dialog. If false,
then no user interaction is allowed.
The value of this field can be set by the
setAllowUserInteraction method.
Its value is returned by the
getAllowUserInteraction method.
Its default value is the value of the argument in the last invocation
of the setDefaultAllowUserInteraction method.
Properties Inheritable useCaches=boolean
true, the protocol is allowed to use caching
whenever it can. If false, the protocol must always
try to get a fresh copy of the object.
This field is set by the setUseCaches method. Its
value is returned by the getUseCaches method.
Its default value is the value given in the last invocation of the
setDefaultUseCaches method.
Properties Inheritable ifModifiedSince=long
A nonzero value gives a time as the number of seconds since January 1, 1970, GMT. The object is fetched only if it has been modified more recently than that time.
This variable is set by the setIfModifiedSince
method. Its value is returned by the
getIfModifiedSince method.
The default value of this field is 0, indicating
that the fetching must always occur.
Properties Public Static
fileNameMap=FileNameMap
Properties Inheritable connected=boolean
false, this connection object has not created a
communications link to the specified URL. If true,
the communications link has been established.
method URLConnection(url=URL) Inheritable
method connect() public abstract Signals IOException
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.
method getURL() returns URL public
URLConnection's URL
field.
URLConnection's URL
field.
method getContentLength() returns int public
content-length header field.
-1 if the content length is
not known.
method getContentType() returns String public
content-type header field.
null if not known.
method getContentEncoding() returns String public
content-encoding header field.
null if not known.
method getExpiration() returns long public
expires header field.
method getDate() returns long public
date header field.
0 if not known. The value returned is the
number of seconds since January 1, 1970 GMT.
method getLastModified() returns long public
last-modified header field.
The result is the number of seconds since January 1, 1970 GMT.
URLConnection was last modified, or 0 if not known.
method getHeaderField(name=String) returns String public
null
if there is no such field in the header.
method getHeaderFieldInt(name=String, -
Default=int) returns int public
This form of getHeaderField exists because some
connection types (e.g., http-ng) have pre-parsed
headers. Classes for that connection type can override this method
and short-circuit the parsing.
Default value is returned if the field is
missing or malformed.
method getHeaderFieldDate(name=String, -
Default=long) returns long public
This form of getHeaderField exists because some
connection types (e.g., http-ng) have pre-parsed
headers. Classes for that connection type can override this method
and short-circuit the parsing.
Default argument is returned if the field is
missing or malformed.
method getHeaderFieldKey(n=int) returns String public
nth header field.
nth header field,
or null if there are fewer than n
fields.
method getHeaderField(n=int) returns String public
nth header field.
It returns null if there are fewer than
n fields.
This method can be used in conjunction with the
getHeaderFieldKey method to iterate through all
the headers in the message.
nth header field.
method getContent() returns Object public Signals IOException
This method first determines the content type of the object by
calling the getContentType method. If this is
the first time that the application has seen that specific content
type, a content handler for that content type is created:
setContentHandlerFactory method, the
createContentHandler method of that instance is called
with the content type as an argument; the result is a content
handler for that content type.
createContentHandler method returns
null, then the application loads the class named:
sun.net.www.content.<contentType>
period ('.'), and all other non-alphanumeric characters
with the underscore character '_'. The alphanumeric
characters are specifically the 26 uppercase ASCII letters
'A' through 'Z', the 26 lowercase ASCII
letters 'a' through 'z', and the 10 ASCII
digits '0' through '9'. If the specified
class does not exist, or is not a subclass of
ContentHandler, then an
UnknownServiceException is thrown.
instanceOf operation
should be used to determine the specific kind of object
returned.
method getInputStream() returns InputStream public Signals IOException
method getOutputStream() returns OutputStream public Signals IOException
method toString() returns String public
String representation of this URL connection.
URLConnection.
method setDoInput(doinput=boolean) public
doInput field for this
URLConnection to the specified value.
A URL connection can be used for input and/or output. Set the DoInput flag to true if you intend to use the URL connection for input, false if not. The default is true unless DoOutput is explicitly set to true, in which case DoInput defaults to false.
method getDoInput() returns boolean public
URLConnection's
doInput flag.
URLConnection's
doInput flag.
method setDoOutput(dooutput=boolean) public
doOutput field for this
URLConnection to the specified value.
A URL connection can be used for input and/or output. Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.
method getDoOutput() returns boolean public
URLConnection's
doOutput flag.
URLConnection's
doOutput flag.
method setAllowUserInteraction(allowuserinteraction=boolean) public
allowUserInteraction field of
this URLConnection.
method getAllowUserInteraction() returns boolean public
allowUserInteraction field for
this object.
allowUserInteraction field for
this object.
method setDefaultAllowUserInteraction(defaultallowuserinteraction=boolean) public static
allowUserInteraction field for all future
URLConnection objects to the specified value.
method getDefaultAllowUserInteraction() returns boolean public static
allowUserInteraction
field.
Ths default is "sticky", being a part of the static state of all URLConnections. This flag applies to the next, and all following URLConnections that are created.
allowUserInteraction
field.
method setUseCaches(usecaches=boolean) public
useCaches field of this
URLConnection to the specified value.
Some protocols do caching of documents. Occasionally, it is important to be able to "tunnel through" and ignore the caches (e.g., the "reload" button in a browser). If the UseCaches flag on a connection is true, the connection is allowed to use whatever caches it can. If false, caches are to be ignored. The default value comes from DefaultUseCaches, which defaults to true.
method getUseCaches() returns boolean public
URLConnection's
useCaches field.
URLConnection's
useCaches field.
method setIfModifiedSince(ifmodifiedsince=long) public
ifModifiedSince field of
this URLConnection to the specified value.
method getIfModifiedSince() returns long public
ifModifiedSince field.
ifModifiedSince field.
method getDefaultUseCaches() returns boolean public
URLConnection's
useCaches flag.
Ths default is "sticky", being a part of the static state of all URLConnections. This flag applies to the next, and all following URLConnections that are created.
URLConnection's
useCaches flag.
method setDefaultUseCaches(defaultusecaches=boolean) public
useCaches field to the
specified value.
method setRequestProperty(key=String, -
value=String) public
accept").
method getRequestProperty(key=String) returns String public
method setDefaultRequestProperty(key=String, -
value=String) public static
URLConnection is created, it is initialized with
these properties.
accept").
method getDefaultRequestProperty(key=String) returns String public static
method setContentHandlerFactory(fac=ContentHandlerFactory) public static protect
ContentHandlerFactory of an
application. It can be called at most once by an application.
The ContentHandlerFactory instance is used to
construct a content handler from a content type
method guessContentTypeFromName(fname=String) returns String Inheritable static
getContentType method.
method guessContentTypeFromStream(is=InputStream) returns String public static Signals IOException
getContentType method.
Ideally, this routine would not be needed. But many
http servers return the incorrect content type; in
addition, there are many nonstandard extensions. Direct inspection
of the bytes to determine the content type is often more accurate
than believing the content type claimed by the http server.
null if none
can be determined.
All Packages Class Hierarchy This Package Previous Next Index