Package java.net |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
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.
protected boolean allowUserInteractionIf 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
protected boolean connectedIf false, this connection object has not created a communications link to the specified URL. If true, the communications link has been established.
protected boolean doInputThis 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.
protected boolean doOutputThis 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.
protected long ifModifiedSinceSome 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.
protected URL urlThe 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 .
protected boolean useCachesIf 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 .
protected URLConnection(URL url)Constructs a URL connection to the specified URL. A connection to the object referenced by the URL is not created.
Parameter Description url the specified URL
public abstract void connect()throws IOExceptionOpens 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:
If an IO error occurs while opening the connection.
public boolean getAllowUserInteraction()Return Value:
Returns the value of the allowUserInteraction field for this object..
public Object getContent()throws IOExceptionRetrieves 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:
- 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.
- 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:
If an IO error occurs while getting the content.
Throws:
If the protocol does not support the content type.
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.
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.
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.
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.
public static boolean getDefaultAllowUserInteraction()Return Value:
Returns The default value of the allowUserInteraction field.
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 .
public boolean getDefaultUseCaches()Return Value:
Returns the default value of this URLConnection's useCaches flag.
public boolean getDoInput()Return Value:
Returns the value of this URLConnection's doInput flag.
public boolean getDoOutput()Return Value:
Returns the value of this URLConnection's doOutput flag.
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.
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.
Parameter Description n an index
public String getHeaderField(String name)Return Value:
Returns the value of the named header field, or null if not known.
Parameter Description name the name of a header field
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.
Parameter Description name the name of the header field Default a default value
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.
Parameter Description name the name of the header field Default the default value
public String getHeaderFieldKey(int n)Return Value:
Returns the key for the nth header field, or null if there are fewer than n fields.
Parameter Description n an index
public long getIfModifiedSince()Return Value:
Returns the value of this object's ifModifiedSince field.
public InputStream getInputStream()throws IOExceptionReturn Value:
Returns an input stream that reads from this open connection.
Throw:
If an IO error occurs while creating the input stream.
Throw:
If the protocol does not support input.
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.
public OutputStream getOutputStream()throws IOExceptionReturn Value:
Returns an output stream that writes to this connection.
Throw:
If an IO error occurs while creating the output stream.
Throw:
If the protocol does not support output.
public String getRequestProperty(String key)Return Value:
Returns The value of the named general request property for this connection.
public URL getURL()Return Value:
Returns the value of this URLConnection's URL field.
public boolean getUseCaches()Return Value:
Returns the value of this URLConnection's useCaches field
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.
Parameter Description fname a file name
protected static String guessContentTypeFromStream(InputStream is)throws IOExceptionTries 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.
Parameter Description is an input stream that supports marks.(that is, it must be built on top of a BufferedInputStream) Throw:
If an IO error occurs while reading the input stream.
public voidsetAllowUserInteraction(boolean allowuserinteraction)Set the value of the allowUserInteraction field of this URLConnection.
Parameter Description allowuserinteraction the new value
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
Parameter Description fac the desired factory Throw:
If the factory has already been defined.
public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)Sets the default value of the allow-User-Interaction flag for this URLConnection to the specified value.
Parameter Description defaultallowuserinteraction the new value
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.
Parameter Description key the keyword by which the request is known (eg "accept") value the value associated with the key.
public void setDefaultUseCaches(boolean defaultusecaches)Sets the default value of the useCaches flag for this URLConnection to the specified value.
Parameter Description defaultusecaches the new value
public void setDoInput(boolean doinput)Sets the value of the doInput flag for this URLConnection to the specified value.
Parameter Description value the new value
public void setDoOutput(boolean dooutput)Sets the value of the doOutput flag for this URL-Connection to the specified value.
Parameter Description value the new value
public void setIfModifiedSince(long ifmodifiedsince)Sets the value of the ifModifiedSince field of this URLConnection to the specified value
Parameter Description value the new value
public void setRequestProperty(String key, String value)Sets the general request property.
Parameter Description key the keyword by which the request is known (eg "accept") value the value associated with it
public void setUseCaches(boolean usecaches)Sets the value of the useCaches field of this URL-Connection to the specified value.
public String toString()Return Value:
Returns a string representation of this URLConnection.
Overrides:
toString in class Object .