urlconnection.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 855 行 · 第 1/2 页
JAVA
855 行
/*************************************************************************/
/**
* Returns a boolean flag indicating whether or not output will be done
* on this connection. The default value is false, so this method can
* be used to override the default
*
* @param output ture if output is to be done, false otherwise
*/
public void setDoOutput(boolean output) {
doOutput = output;
}
/*************************************************************************/
/**
* Returns a boolean flag indicating whether or not caching will be used
* (if possible) to store data downloaded via the connection.
*
* @return true if caching should be used if possible, false otherwise
*/
public boolean getUseCaches() {
return (useCaches);
}
/*************************************************************************/
/**
* Sets a boolean flag indicating whether or not caching will be used
* (if possible) to store data downloaded via the connection.
*
* @param use_cache true if caching should be used if possible, false otherwise
*/
public void setUseCaches(boolean use_caches) {
useCaches = use_caches;
}
/*************************************************************************/
/**
* Returns the ifModified since instance variable. If this value is non
* zero and the underlying protocol supports it, the actual document will
* not be fetched unless it has been modified since this time. The value
* returned will be 0 if this feature is disabled or the time expressed
* as the number of seconds since midnight 1/1/1970 GMT otherwise
*
* @return The ifModifiedSince value
*/
public long getIfModifiedSince() {
return (ifModifiedSince);
}
/*************************************************************************/
/**
* Sets the ifModified since instance variable. If this value is non
* zero and the underlying protocol supports it, the actual document will
* not be fetched unless it has been modified since this time. The value
* passed should be 0 if this feature is to be disabled or the time expressed
* as the number of seconds since midnight 1/1/1970 GMT otherwise.
*
* @param modified_since The new ifModifiedSince value
*/
public void setIfModifiedSince(long modified_since) {
ifModifiedSince = modified_since;
}
/*************************************************************************/
/**
* Returns the value of the named request property.
*
* @param key The name of the property
*
* @return The value of the property
*/
public String getRequestProperty(String key) {
return ((String) req_props.get(key.toLowerCase()));
}
/*************************************************************************/
/**
* Sets the value of the named request property
*
* @param key The name of the property
* @param value The value of the property
*/
public void setRequestProperty(String key, String value) {
req_props.put(key.toLowerCase(), value);
}
/**
* Returns an unmodifiable Map containing the request properties.
*
* @since 1.4
*/
public Map getRequestProperties() {
return Collections.unmodifiableMap(req_props);
}
/*************************************************************************/
/**
* Returns the URL object associated with this connection
*
* @return The URL for this connection.
*/
public URL getURL() {
return (url);
}
/*************************************************************************/
/**
* Establishes the actual connection to the URL associated with this
* connection object
*/
public abstract void connect() throws IOException;
/*************************************************************************/
/**
* Returns an InputStream for this connection. As this default
* implementation returns null, subclasses should override this method
*
* @return An InputStream for this connection
*
* @exception IOException If an error occurs
*/
public InputStream getInputStream() throws IOException {
return (null);
}
/*************************************************************************/
/**
* Returns an OutputStream for this connection. As this default
* implementation returns null, subclasses should override this method
*
* @return An OutputStream for this connection
*
* @exception IOException If an error occurs
*/
public OutputStream getOutputStream() throws IOException {
return (null);
}
/*************************************************************************/
/**
* Returns the value of the content-encoding field or null if it is not
* known or not present.
*
* @return The content-encoding field
*/
public String getContentEncoding() {
return (getHeaderField("content-encoding"));
}
/*************************************************************************/
/**
* Returns the value of the content-length header field or -1 if the value
* is not known or not present.
*
* @return The content-length field
*/
public int getContentLength() {
return (getHeaderFieldInt("content-length", -1));
}
/*************************************************************************/
/**
* Returns the the content-type of the data pointed to by the URL. This
* method first tries looking for a content-type header. If that is not
* present, it attempts to use the file name to determine the content's
* MIME type. If that is unsuccessful, the method returns null. The caller
* may then still attempt to determine the MIME type by a call to
* guessContentTypeFromStream()
*
* @return The content MIME type
*/
public String getContentType() {
String type = getHeaderField("content-type");
if (type == null)
type = guessContentTypeFromName(getURL().getFile());
return (type);
}
/*************************************************************************/
/**
* Returns the date of the document pointed to by the URL as reported in
* the date field of the header or 0 if the value is not present or not
* known. If populated, the return value is number of seconds since
* midnight on 1/1/1970 GMT.
*
* @return The document date
*/
public long getDate() {
return (getHeaderFieldDate("date", 0));
}
/*************************************************************************/
/**
* Returns the value of the expires header or 0 if not known or present.
* If populated, the return value is number of seconds since midnight
* on 1/1/1970 GMT.
*
* @return The expiration time.
*/
public long getExpiration() {
return (getHeaderFieldDate("expires", 0));
}
/*************************************************************************/
/**
* Returns the value of the last-modified header field or 0 if not known known
* or not present. If populated, the return value is the number of seconds
* since midnight on 1/1/1970.
*
* @return The last modified time
*/
public long getLastModified() {
return (getHeaderFieldDate("last-modified", 0));
}
/*************************************************************************/
/**
* Returns a String representing the header key at the specified index.
* This allows the caller to walk the list of header fields. The analogous
* getHeaderField(int) method allows access to the corresponding value for
* this tag.
*
* @param index The index into the header field list to retrieve the key for.
*
* @return The header field key or null if index is past the end of the headers
*/
public String getHeaderFieldKey(int index) {
return (null);
}
/*************************************************************************/
/**
* Return a String representing the header value at the specified index.
* This allows the caller to walk the list of header fields. The analogous
* getHeaderFieldKey(int) method allows access to the corresponding key
* for this header field
*
* @param index The index into the header field list to retrieve the value for
*
* @return The header value or null if index is past the end of the headers
*/
public String getHeaderField(int index) {
return (null);
}
/*************************************************************************/
/**
* Returns a String representing the value of the header field having
* the named key. Returns null if the header field does not exist.
*
* @param The key of the header field
*
* @return The value of the header field as a String
*/
public String getHeaderField(String name) {
for (int i = 0;; i++) {
String key = getHeaderFieldKey(i);
if (key == null)
return (null);
if (key.toLowerCase().equals(name.toLowerCase()))
return (getHeaderField(i));
}
}
/*************************************************************************/
/**
* Returns the value of the named header field as a date. This date will
* be the number of seconds since midnight 1/1/1970 GMT or the default
* value if the field is not present or cannot be converted to a date.
*
* @param key The header field key to lookup
* @param def The default value if the header field is not found or can't be converted
*/
public long getHeaderFieldDate(String key, long def) {
String value = getHeaderField(key);
if (value == null)
return (def);
// This needs to change since Date(String) is deprecated, but DateFormat
// doesn't seem to be working for some reason
//DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
//df.setLenient(true);
//Date d = df.parse(value, new ParsePosition(0));
Date d = new Date(value);
if (d == null)
return (def);
return (d.getTime() / 1000);
}
/*************************************************************************/
/**
* Returns the value of the named header field as an int. If the field
* is not present or cannot be parsed as an integer, the default value
* will be returned.
*
* @param key The header field key to lookup
* @param def The defaule value if the header field is not found or can't be parsed
*/
public int getHeaderFieldInt(String key, int def) {
String value = getHeaderField(key);
if (value == null)
return (def);
int retval = def;
try {
retval = Integer.parseInt(value);
} catch (NumberFormatException e) {
return (def);
}
return (retval);
}
/*************************************************************************/
/**
* This method returns a <code>Permission</code> object representing the
* permissions required to access this URL. This method returns
* <code>java.security.AllPermission</code> by default. Subclasses should
* override it to return a more specific permission. For example, an
* HTTP URL should return an instance of <code>SocketPermission</code>
* for the appropriate host and port.
* <p>
* Note that because of items such as HTTP redirects, the permission
* object returned might be different before and after connecting.
*
* @return A Permission object
*
* @exception IOException If an error occurs
*/
public Permission getPermission() throws IOException {
return (new java.security.AllPermission());
}
/*************************************************************************/
/**
* This method returns the content of the document pointed to by the URL
* as an Object. The type of object depends on the MIME type of the
* object and particular content hander loaded. Most text type content
* handlers will return a subclass of InputStream. Images usually return
* a class that implements ImageProducer. There is not guarantee what
* type of object will be returned, however.
* <p>
* This class first determines the MIME type of the content, then creates
* a ContentHandler object to process the input. If the ContentHandlerFactory
* is set, then that object is called to load a content handler, otherwise
* a class called gnu.java.net.content.<content_type> is tried.
* The default class will also be used if the content handler factory returns
* a null content handler.
*
* @exception IOException If an error occurs.
*/
public Object getContent() throws IOException {
// connect();
String type = getContentType();
// First try the factory
ContentHandler ch = null;
if (factory != null)
ch = factory.createContentHandler(type);
if (ch != null)
return (ch.getContent(this));
// Then try our default class
try {
Class cls = Class.forName("gnu.java.net.content." + type.replace('/', '.'));
Object obj = cls.newInstance();
if (!(obj instanceof ContentHandler))
throw new UnknownServiceException(type);
ch = (ContentHandler) obj;
return (ch.getContent(this));
} catch (ClassNotFoundException e) {
// Do nothing
} catch (InstantiationException e) {
// Do nothing
} catch (IllegalAccessException e) {
// Do nothing
}
throw new UnknownServiceException(type);
}
/*************************************************************************/
/**
* The methods prints the value of this object as a String by calling the
* toString() method of its associated URL. Overrides Object.toString()
*
* @return A String representation of this object
*/
public String toString() {
return (url.toString());
}
} // class URLConnection
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?