Packages  This Package  Prev  Next  Index  
	§4.9 Class URLConnection
public  abstract  class  java.net.URLConnection
    extends  java.lang.Object  (I-§1.12)
{
        // Fields
    protected boolean allowUserInteraction;	§4.9.1
    protected boolean connected;	§4.9.2
    protected boolean doInput;	§4.9.3
    protected boolean doOutput;	§4.9.4
    protected long ifModifiedSince;	§4.9.5
    protected URL url;	§4.9.6
    protected boolean useCaches;	§4.9.7
        // Constructors
    protected URLConnection(URL  url);	§4.9.8
        // Methods
    public abstract void connect();	§4.9.9
    public boolean getAllowUserInteraction();	§4.9.10
    public Object getContent();	§4.9.11
    public String getContentEncoding();	§4.9.12
    public int getContentLength();	§4.9.13
    public String getContentType();	§4.9.14
    public long getDate();	§4.9.15
    public static boolean getDefaultAllowUserInteraction();	§4.9.16
    public static String 	§4.9.17
        getDefaultRequestProperty(String  key);
    public boolean getDefaultUseCaches();	§4.9.18
    public boolean getDoInput();	§4.9.19
    public boolean getDoOutput();	§4.9.20
    public long getExpiration();	§4.9.21
    public String getHeaderField(int  n);	§4.9.22
    public String getHeaderField(String  name);	§4.9.23
    public long getHeaderFieldDate(String  name, long  Default); §4.9.24
    public int getHeaderFieldInt(String  name, int  Default);	§4.9.25
    public String getHeaderFieldKey(int  n);	§4.9.26
    public long getIfModifiedSince();	§4.9.27
    public InputStream getInputStream();	§4.9.28
    public long getLastModified();	§4.9.29
    public OutputStream getOutputStream();	§4.9.30
    public String getRequestProperty(String  key);	§4.9.31
    public URL getURL();	§4.9.32
    public boolean getUseCaches();	§4.9.33
    protected static String	§4.9.34
        guessContentTypeFromName(String  fname);
    protected static String	§4.9.35
        guessContentTypeFromStream(InputStream  is);
    public void	§4.9.36
        setAllowUserInteraction(boolean  allowuserinteraction);
    public static void	§4.9.37
        setContentHandlerFactory(ContentHandlerFactory  fac);
    public static void	§4.9.38
     setDefaultAllowUserInteraction(boolean  defaultallowuserinteraction);
    public static void	§4.9.39
        setDefaultRequestProperty(String  key, String  value);
    public void	§4.9.40
        setDefaultUseCaches(boolean  defaultusecaches);
    public void setDoInput(boolean  doinput);	§4.9.41
    public void setDoOutput(boolean  dooutput);	§4.9.42
    public void setIfModifiedSince(long  ifmodifiedsince);	§4.9.43
    public void setRequestProperty(String  key, ;String  value);  §4.9.44
    public void setUseCaches(boolean  usecaches);	§4.9.45
    public String toString();	§4.9.46
}
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: 
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.
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  (I-§4.9.36). Its value can be accessed by the get-Allow-UserInteraction 
method  (I-§4.9.10). Its default value is the value of the argument method 
to the last call to the set--Default-Allow-User-Interaction method (I-§4.9.38)
  
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 (I-§4.9.41). Its value can be 
accessed by the getDoInput method (I-§4.9.19).
- 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 (I-§4.9.42). Its value can be 
accessed by the getDoInput method (I-§4.9.20).
- 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 (I-§4.9.43). Its value 
can be accessed by the getIfModifiedSince method  (I-§4.9.27).
- 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 (I-§4.9.32).
- The default value of this variable is the value of the URL argument in the 
URLConnection constructor (I-§4.9.6).
   
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 (I-§4.9.45). Its value can be 
accessed by the getUseCaches method  (I-§4.9.33). Its default value is the 
value of the last argument to the method set-Default-Use-Caches (I-§4.9.40).
  
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.
- Parameters:
 url
- the specified URL
 
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 (I-§4.9.2) having the value true), 
the call is ignored.
- Throws
 - IOException  (I-§2.29)
- If an IO error occurs while opening the connection.
  
  
getAllowUserInteraction
public boolean getAllowUserInteraction()
- Returns:
 - the value of the allowUserInteraction  (I-§4.9.1) 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  (I-§4.9.1). If this is the first time that the application 
has seen that specific content type, a content handler for that content type 
is created:
- If the application has set up a content handler factory instace using the 
setContentHandlerFactory (I-§4.9.37) method, the create--Content-Handler 
(I-§4.12.1) method of that instance is called with the content type as an 
argument; the result is a content handler for that content type.
 
- 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.
- Returns:
 - the object fetched. The instanceof operation should be used to determine the specific kind of object returned.
 - Throws
 - IOException  (I-§2.29)
- If an IO error occurs while getting the content.
  - Throws
 - UnknownServiceException  (I-§4.19)
- If the protocol does not support the content type.
   
  
getContentEncoding
public String getContentEncoding()
- Determines the value of the content-encoding attribute.
- 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.
- 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.
- 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.
- 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()
- Returns:
 - The default value of the allowUserInteraction (I-§4.9.1) field.
 
getDefaultRequestProperty
public static String getDefaultRequestProperty(String  key)
- Gets the value of the default request property. Default request properties 
are set for every connection.
- Returns:
 - the value of the default rnequest property for the specified key.
 - See Also:
 - setDefaultRequestProperty (I-§4.9.39).
 
 
getDefaultUseCaches
public boolean getDefaultUseCaches()
- Returns:
 - the default value of this URLConnection's useCaches (I-§4.9.7) flag.
 
getDoInput
public boolean getDoInput()
- Returns:
 - the value of this URLConnection's doInput (I-§4.9.3) flag.
 
getDoOutput
public boolean getDoOutput()
- Returns:
 - the value of this URLConnection's doOutput (I-§4.9.4) flag.
 
getExpiration
public long getExpiration()
- Determines the value of the expires attribute.
- 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  (I-§4.9.26)to iterate through all the headers in the message.
- Parameters:
 n
- an index
- Returns:
 - the value of the nth header field.
 
  
getHeaderField
public String getHeaderField(String  name)
- Parameters:
 name
- the name of a header field
- Returns: 
 - the value of the named header field, or null if not known.
 
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.
- Parameters:
 name
- the name of the header field
Default
- a default 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.
 
  
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.
- Parameters:
 name
- the name of the header field
Default
- the default value
- Returns:
 - the value of the named field, parsed as an integer. The Default value is 
returned if the field is missing or malformed.
 
  
getHeaderFieldKey
public String getHeaderFieldKey(int  n)
- Parameters:
 n
- an index
- Returns: 
 - the key for the nth header field, or null if there are fewer than n fields. 
 
getIfModifiedSince
public long getIfModifiedSince()
- Returns:
 - the value of this object's ifModifiedSince  (I-§4.9.5) field.
 
getInputStream
public InputStream getInputStream()
throws IOException
- Returns:
 - an input stream that reads from this open connection.
 - Throws
 - IOException  (I-§2.29)
- If an IO error occurs while creating the input stream.
  - Throws
 - UnknownServiceException  (I-§4.19)
- If the protocol does not support input.
  
getLastModified
public long getLastModified()
- Determines the value of the last-modified attribute.
- 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
- Returns:
 - an output stream that writes to this connection.
 - Throws
 - IOException  (I-§2.29)
- If an IO error occurs while creating the output stream.
  - Throws
 - UnknownServiceException  (I-§4.19)
- If the protocol does not support output.
  
getRequestProperty
public String getRequestProperty(String  key)
- Returns:
 - The value of the named general request property for this connection.
 
getURL
public URL getURL()
- Returns:
 - the value of this URLConnection's URL (I-§4.9.6) field.
 
getUseCaches
public boolean getUseCaches()
- Returns:
 - the value of this URLConnection's useCaches (I-§4.9.7) 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 (I-§4.9.14).
- Parameters:
 fname
- a file name
- Returns:
 - a guess as to what the content type of the object is, based upon its 
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 (I-§4.9.14)
- 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.
- Parameters:
 is
- an input stream that supports marks.(that is, it must be built on top 
of a BufferedInputStream)
- Returns:
 - 	a guess at the content type, or null if none can be determined.
 - Throws
 - IOException  (I-§2.29)
- If an IO error occurs while reading the input stream.
  
  
setAllowUserInteraction
public void 
setAllowUserInteraction(boolean  allowuserinteraction)
- Set the value of the allowUserInteraction (I-§4.9.1) field of this URLConnection.
- Parameters:
 allowuserinteraction
- - the new value
 
 
setContentHandlerFactory
public static void
setContentHandlerFactory(ContentHandlerFactory  fac)
- Sets the ContentHandlerFactory  (I-§4.12) of an application. It can be called at 
most once by an application.
- The ContentHandlerFactory instance is used to construct a content 
handler  (I-§4.9.11) from a content type
- Parameters:
 fac
- the desired factory
- Throws
 - Error  (I-§1.48)
- If the factory has already been defined.
  
  
setDefaultAllowUserInteraction
public static void  setDefaultAllowUserInteraction(
        boolean  defaultallowuserinteraction)
- Sets the default value of the allow-User-Interaction (I-§4.9.1) flag for this URLConnection to the specified value.
- Parameters:
 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.
- Parameters:
 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 (I-§4.9.7) flag for this URLConnection 
to the specified value.
- Parameters:
 defaultusecaches
- - the new value
 
 
setDoInput
public void setDoInput(boolean  doinput)
- Sets the value of the doInput (I-§4.9.3) flag for this URLConnection to the 
specified value.
- Parameters:
 value
- the new value
 
setDoOutput
public void setDoOutput(boolean  dooutput)
- Sets the value of the doOutput (I-§4.9.4) flag for this URL-Connection to the 
specified value.
- Parameters:
 value
- the new value
 
setIfModifiedSince
public void setIfModifiedSince(long  ifmodifiedsince)
- Sets the value of the ifModifiedSince  (I-§4.9.5) field of this URLConnection to 
the specified value
- Parameters:
 value
- the new value
 
setRequestProperty
public void setRequestProperty(String  key, String  value)
- Sets the general request property.
- Parameters:
 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 (I-§4.9.7) field of this URL-Connection to the 
specified value.
 
toString
public String toString()
- Returns:
 - a string representation of this URLConnection.
 - Overrides:
 - toString in class Object  (I-§1.12.9).
 
Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc.
All rights reserved
Please send any comments or corrections to doug.kramer@sun.com