Package java.net Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class URLConnection

Fields , Constructors , Methods

public  abstract  class  java.net.URLConnection
    extends  java.lang.Object  
{
        // Fields
    protected boolean allowUserInteraction;	
    protected boolean connected;	
    protected boolean doInput;	
    protected boolean doOutput;	
    protected long ifModifiedSince;	
    protected URL url;	
    protected boolean useCaches;	

        // Constructors
    protected URLConnection(URL  url);	

        // Methods
    public abstract void connect();	
    public boolean getAllowUserInteraction();	
    public Object getContent();	
    public String getContentEncoding();	
    public int getContentLength();	
    public String getContentType();	
    public long getDate();	
    public static boolean getDefaultAllowUserInteraction();	
    public static String 	
        getDefaultRequestProperty(String  key);
    public boolean getDefaultUseCaches();	
    public boolean getDoInput();	
    public boolean getDoOutput();	
    public long getExpiration();	
    public String getHeaderField(int  n);	
    public String getHeaderField(String  name);	
    public long getHeaderFieldDate(String  name, long  Default); 
    public int getHeaderFieldInt(String  name, int  Default);	
    public String getHeaderFieldKey(int  n);	
    public long getIfModifiedSince();	
    public InputStream getInputStream();	
    public long getLastModified();	
    public OutputStream getOutputStream();	
    public String getRequestProperty(String  key);	
    public URL getURL();	
    public boolean getUseCaches();	
    protected static String	
        guessContentTypeFromName(String  fname);
    protected static String	
        guessContentTypeFromStream(InputStream  is);
    public void	
        setAllowUserInteraction(boolean  allowuserinteraction);
    public static void	
        setContentHandlerFactory(ContentHandlerFactory  fac);
    public static void	
     setDefaultAllowUserInteraction(boolean  defaultallowuserinteraction);
    public static void	
        setDefaultRequestProperty(String  key, String  value);
    public void	
        setDefaultUseCaches(boolean  defaultusecaches);
    public void setDoInput(boolean  doinput);	
    public void setDoOutput(boolean  dooutput);	
    public void setIfModifiedSince(long  ifmodifiedsince);	
    public void setRequestProperty(String  key, ;String  value);  
    public void setUseCaches(boolean  usecaches);	
    public String toString();	
}

The abstract class 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 multi-step process.

First, the connection object is created by invoking openConnection on a URL, then setup parameters are manipulated, then the actual connection to the remote object is made, and finally the remote object is available. There are a set of properties that apply before the connection is made, and a set that apply after:
before connect after connect
AllowUserInteraction ContentEncoding
RequestProperty ContentLength
UseCaches ContentType
DoOutput HeaderField
DoInput InputStream
IfModifiedSince OutputStream
LastModified
Date
LastModified
Expiration

Most of these properties have a set and a get method. The first three in the "before" column also have corresponding "Default" properties that specify the default values used in subsequent requests where the property is not specified.

In the common case, all of these properties can be ignored: the pre-connection properties default to sensible values, and the post-connection properties are often uninteresting. 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.


Fields


allowUserInteraction

protected boolean allowUserInteraction 

If true, this URL is being examined in a context which it makes sense to allow user interactions such as popping up an authentication dialog. If false, then no user interaction is allowed.

This value of this field can be set by the set-Allow-User-Interaction method . Its value can be accessed by the get-Allow-UserInteraction method . Its default value is the value of the argument method to the last call to the set--Default-Allow-User-Interaction method


connected

protected boolean connected 

If false, this connection object has not created a communications link to the specified URL. If true, the communications link has been established.


doInput

protected boolean doInput 

This variable is set by the setDoInput method . Its value can be accessed by the getDoInput method .

A URL connection can be used for input and/or output. Setting the do-Input flag to true indicates that the application intends to read data from the URL connection.

The default value of this field is true.


doOutput

protected boolean doOutput 

This variable is set by the setDoOutput method . Its value can be accessed by the getDoInput method .

A URL connection can be used for input and/or output. Setting the do-Output flag to true indicates that the application intends to write data to the URL connection.

The default value of this field is false.


ifModifiedSince

protected long ifModifiedSince 

Some protocols support skipping the fetching of the object unless the object has been modified more recently than a certain time.

A non-zero value is interpreted as the time corresponding to if-Modified-Since 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 can be accessed by the getIfModifiedSince method .

The default value of this field is 0, indicating that the fetching must always occur.


url

protected URL url 

The URL representing the remote object on the World Wide Web to which this connection is opened.

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 .


useCaches

protected boolean useCaches 

If 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 can be accessed by the getUseCaches method . Its default value is the value of the last argument to the method set-Default-Use-Caches .


Constructors


URLConnection

protected URLConnection(URL  url) 

Constructs a URL connection to the specified URL. A connection to the object referenced by the URL is not created.

ParameterDescription
url the specified URL


Methods


connect

public abstract void connect() 
throws IOException 

Opens a communications link to the resource referenced by this URL, if such a connection hasn't already been established.

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

Throw:

IOException

If an IO error occurs while opening the connection.


getAllowUserInteraction

public boolean getAllowUserInteraction() 

Return Value:

Returns the value of the allowUserInteraction field for this object..


getContent

public Object getContent() 
throws IOException 

Retrieves the contents of this URL connection.

This method first determines the content type of the object by calling the method getContentType . If this is the first time that the application has seen that specific content type, a content handler for that content type is created:

  1. If the application has set up a content handler factory instace using the setContentHandlerFactory method, the create--Content-Handler method of that instance is called with the content type as an argument; the result is a content handler for that content type.
  2. If no content handler factory has yet been set up, or if the factory's createContentHandler method returns null, then the application loads the class named

    sun.net.www.content.<contentType>

    where contentType is formed by taking the content type string, replacing all slash characters with a period '.', and all other non-alphanumeric characters with the underscore character '_'. If the specified class does not exist, or is not a subclass of ContentHandler, then an UnknownServiceException is thrown.

Return Value:

Returns the object fetched. The instanceof operation should be used to determine the specific kind of object returned.

Throws:

IOException

If an IO error occurs while getting the content.

Throws:

UnknownServiceException

If the protocol does not support the content type.


getContentEncoding

public String getContentEncoding() 

Determines the value of the content-encoding attribute.

Return Value:

Returns the content encoding of the resource that the URL references, or null if not known.


getContentLength

public int getContentLength() 

Determines the value of the content-length attribute.

Return Value:

Returns the content length of the resource that this connection's URL referncees, or -1 if the content length is not known.


getContentType

public String getContentType() 

Determines the value of the content-type attribute.

Return Value:

Returns the content type of the resource that the URL references, or null if not known.


getDate

public long getDate() 

Determines the value of the date attribute.

Return Value:

Returns the sending date of the resource that the URL refernces, or 0 if not known. The value returned is the number of seconds since January 1, 1970 GMT.


getDefaultAllowUserInteraction

public static boolean getDefaultAllowUserInteraction() 

Return Value:

Returns The default value of the allowUserInteraction field.


getDefaultRequestProperty

public static String getDefaultRequestProperty(String  key) 

Gets the value of the default request property. Default request properties are set for every connection.

Return Value:

Returns the value of the default rnequest property for the specified key.

See Also: setDefaultRequestProperty .


getDefaultUseCaches

public boolean getDefaultUseCaches() 

Return Value:

Returns the default value of this URLConnection's useCaches flag.


getDoInput

public boolean getDoInput() 

Return Value:

Returns the value of this URLConnection's doInput flag.


getDoOutput

public boolean getDoOutput() 

Return Value:

Returns the value of this URLConnection's doOutput flag.


getExpiration

public long getExpiration() 

Determines the value of the expires attribute.

Return Value:

Returns the expiration date of the resource that this URL references, or 0 if not known. The value is number of seconds since January 1, 1970 GMT.


getHeaderField

public String getHeaderField(int  n) 

Gets the value for the 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.

Return Value:

Returns the value of the nth header field.

ParameterDescription
n an index


getHeaderField

public String getHeaderField(String  name) 

Return Value:

Returns the value of the named header field, or null if not known.

ParameterDescription
name the name of a header field


getHeaderFieldDate

public long getHeaderFieldDate(String  name, long  Default) 

The named header field is parsed as a date. The result is the number of seconds since January 1, 1970 GMT represented by the named field.

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.

Return Value:

Returns the value of the field, parsed as a date. The value of the Default argument is returned if the field is missing or malformed.

ParameterDescription
name the name of the header field
Default a default value


getHeaderFieldInt

public int getHeaderFieldInt(String  name, int  Default) 

The named header field is parsed as a number. The value is returned.

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.

Return Value:

Returns the value of the named field, parsed as an integer. The Default value is returned if the field is missing or malformed.

ParameterDescription
name the name of the header field
Default the default value


getHeaderFieldKey

public String getHeaderFieldKey(int  n) 

Return Value:

Returns the key for the nth header field, or null if there are fewer than n fields.

ParameterDescription
n an index


getIfModifiedSince

public long getIfModifiedSince() 

Return Value:

Returns the value of this object's ifModifiedSince field.


getInputStream

public InputStream getInputStream() 
throws IOException 

Return Value:

Returns an input stream that reads from this open connection.

Throw:

IOException

If an IO error occurs while creating the input stream.

Throw:

UnknownServiceException

If the protocol does not support input.


getLastModified

public long getLastModified() 

Determines the value of the last-modified attribute.

Return Value:

Returns the date the resource referenced by this URLConnection was last modified, or 0 if not known. The result is the number of seconds since January 1, 1970 GMT.


getOutputStream

public OutputStream getOutputStream() 
throws IOException 

Return Value:

Returns an output stream that writes to this connection.

Throw:

IOException

If an IO error occurs while creating the output stream.

Throw:

UnknownServiceException

If the protocol does not support output.


getRequestProperty

public String getRequestProperty(String  key) 

Return Value:

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


getURL

public URL getURL() 

Return Value:

Returns the value of this URLConnection's URL field.


getUseCaches

public boolean getUseCaches() 

Return Value:

Returns the value of this URLConnection's useCaches field


guessContentTypeFromName

protected static String guessContentTypeFromName(String  fname) 

Makes a guess as to what the content type of an object is, based upon the argument, which should be the "information" component of a URL. This is a convenience method which can be used by subclasses which override the getContentType method .

Return Value:

Returns a guess as to what the content type of the object is, based upon its name.

ParameterDescription
fname a file name


guessContentTypeFromStream

protected static String guessContentTypeFromStream(InputStream  is) 
throws IOException 

Tries to determine the type of an input stream based on the characters at the beginning of the input stream. This is a convenience method which can be used by subclasses which override the getContentType method

Ideally, this routine would not be needed. But many http servers return the incorrect content type; in addition, there are many non-standard 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.

Return Value:

Returns a guess at the content type, or null if none can be determined.

ParameterDescription
is an input stream that supports marks.(that is, it must be built on top of a BufferedInputStream)

Throw:

IOException

If an IO error occurs while reading the input stream.


setAllowUserInteraction

public void 
setAllowUserInteraction(boolean  allowuserinteraction) 

Set the value of the allowUserInteraction field of this URLConnection.

ParameterDescription
allowuserinteraction
the new value


setContentHandlerFactory

public static void
setContentHandlerFactory(ContentHandlerFactory  fac) 

Sets the 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

ParameterDescription
fac the desired factory

Throw:

Error

If the factory has already been defined.


setDefaultAllowUserInteraction

public static void  setDefaultAllowUserInteraction( 
        boolean  defaultallowuserinteraction) 

Sets the default value of the allow-User-Interaction flag for this URLConnection to the specified value.

ParameterDescription
defaultallowuserinteraction
the new value


setDefaultRequestProperty

public static void
setDefaultRequestProperty(String  key, String  value) 

Sets the default value of a general request property. When a URL-Connection is created, it is initialized with these properties.

ParameterDescription
key the keyword by which the request is known (eg "accept")
value the value associated with the key.


setDefaultUseCaches

public void setDefaultUseCaches(boolean  defaultusecaches) 

Sets the default value of the useCaches flag for this URLConnection to the specified value.

ParameterDescription
defaultusecaches
the new value


setDoInput

public void setDoInput(boolean  doinput) 

Sets the value of the doInput flag for this URLConnection to the specified value.

ParameterDescription
value the new value


setDoOutput

public void setDoOutput(boolean  dooutput) 

Sets the value of the doOutput flag for this URL-Connection to the specified value.

ParameterDescription
value the new value


setIfModifiedSince

public void setIfModifiedSince(long  ifmodifiedsince) 

Sets the value of the ifModifiedSince field of this URLConnection to the specified value

ParameterDescription
value the new value


setRequestProperty

public void setRequestProperty(String  key, String  value) 

Sets the general request property.

ParameterDescription
key the keyword by which the request is known (eg "accept")
value the value associated with it


setUseCaches

public void setUseCaches(boolean  usecaches) 

Sets the value of the useCaches field of this URL-Connection to the specified value.


toString

public String toString() 

Return Value:

Returns a string representation of this URLConnection.

Overrides:

toString in class Object .



Top© 1996 Sun Microsystems, Inc. All rights reserved.