urlconnection.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 1,104 行 · 第 1/3 页
JAVA
1,104 行
/**
* Returns the value of the <code>content-encoding</code> header field.
*
* @return the content encoding of the resource that the URL references,
* or <code>null</code> if not known.
* @see java.net.URLConnection#getHeaderField(java.lang.String)
* @since JDK1.0
*/
public String getContentEncoding() {
return getHeaderField("content-encoding");
}
/**
* Returns the value of the <code>expires</code> header field.
*
* @return the expiration date of the resource that this URL references,
* or 0 if not known. The value is the number of seconds since
* January 1, 1970 GMT.
* @see java.net.URLConnection#getHeaderField(java.lang.String)
* @since JDK1.0
*/
public long getExpiration() {
return getHeaderFieldDate("expires", 0);
}
/**
* Returns the value of the <code>date</code> header field.
*
* @return the sending date of the resource that the URL references,
* or <code>0</code> if not known. The value returned is the
* number of seconds since January 1, 1970 GMT.
* @see java.net.URLConnection#getHeaderField(java.lang.String)
* @since JDK1.0
*/
public long getDate() {
return getHeaderFieldDate("date", 0);
}
/**
* Returns the value of the <code>last-modified</code> header field.
* The result is the number of seconds since January 1, 1970 GMT.
*
* @return the date the resource referenced by this
* <code>URLConnection</code> was last modified, or 0 if not known.
* @see java.net.URLConnection#getHeaderField(java.lang.String)
* @since JDK1.0
*/
public long getLastModified() {
return getHeaderFieldDate("last-modified", 0);
}
/**
* Returns the value of the specified header field. Names of
* header fields to pass to this method can be obtained from
* getHeaderFieldKey.
*
* @param name the name of a header field.
* @return the value of the named header field, or <code>null</code>
* if there is no such field in the header.
* @see java.net.URLConnection#getHeaderFieldKey(int)
* @since JDK1.0
*/
public String getHeaderField(String name) {
return null;
}
/**
* Returns the value of the named field parsed as a number.
* <p>
* This form of <code>getHeaderField</code> exists because some
* connection types (e.g., <code>http-ng</code>) have pre-parsed
* headers. Classes for that connection type can override this method
* and short-circuit the parsing.
*
* @param name the name of the header field.
* @param Default the default value.
* @return the value of the named field, parsed as an integer. The
* <code>Default</code> value is returned if the field is
* missing or malformed.
* @since JDK1.0
*/
public int getHeaderFieldInt(String name, int Default) {
try {
return Integer.parseInt(getHeaderField(name));
} catch(Throwable t) {}
return Default;
}
/**
* Returns the value of the named field parsed as date.
* The result is the number of seconds since January 1, 1970 GMT
* represented by the named field.
* <p>
* This form of <code>getHeaderField</code> exists because some
* connection types (e.g., <code>http-ng</code>) have pre-parsed
* headers. Classes for that connection type can override this method
* and short-circuit the parsing.
*
* @param name the name of the header field.
* @param Default a default value.
* @return the value of the field, parsed as a date. The value of the
* <code>Default</code> argument is returned if the field is
* missing or malformed.
* @since JDK1.0
*/
public long getHeaderFieldDate(String name, long Default) {
try {
return Date.parse(getHeaderField(name));
} catch(Throwable t) {}
return Default;
}
/**
* Returns the key for the <code>n</code><sup>th</sup> header field.
*
* @param n an index.
* @return the key for the <code>n</code><sup>th</sup> header field,
* or <code>null</code> if there are fewer than <code>n</code>
* fields.
* @since JDK1.0
*/
public String getHeaderFieldKey(int n) {
return null;
}
/**
* Returns the value for the <code>n</code><sup>th</sup> header field.
* It returns <code>null</code> if there are fewer than
* <code>n</code> fields.
* <p>
* This method can be used in conjunction with the
* <code>getHeaderFieldKey</code> method to iterate through all
* the headers in the message.
*
* @param n an index.
* @return the value of the <code>n</code><sup>th</sup> header field.
* @see java.net.URLConnection#getHeaderFieldKey(int)
* @since JDK1.0
*/
public String getHeaderField(int n) {
return null;
}
/**
* Retrieves the contents of this URL connection.
* <p>
* This method first determines the content type of the object by
* calling the <code>getContentType</code> 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:
* <ol>
* <li>If the application has set up a content handler factory instance
* using the <code>setContentHandlerFactory</code> method, the
* <code>createContentHandler</code> method of that instance is called
* with the content type as an argument; the result is a content
* handler for that content type.
* <li>If no content handler factory has yet been set up, or if the
* factory's <code>createContentHandler</code> method returns
* <code>null</code>, then the application loads the class named:
* <ul><code>
* sun.net.www.content.<<i>contentType</i>>
* </code></ul>
* where <<i>contentType</i>> is formed by taking the
* content-type string, replacing all slash characters with a
* <code>period</code> ('.'), and all other non-alphanumeric characters
* with the underscore character '<code>_</code>'. The alphanumeric
* characters are specifically the 26 uppercase ASCII letters
* '<code>A</code>' through '<code>Z</code>', the 26 lowercase ASCII
* letters '<code>a</code>' through '<code>z</code>', and the 10 ASCII
* digits '<code>0</code>' through '<code>9</code>'. If the specified
* class does not exist, or is not a subclass of
* <code>ContentHandler</code>, then an
* <code>UnknownServiceException</code> is thrown.
* </ol>
*
* @return the object fetched. The <code>instanceOf</code> operation
* should be used to determine the specific kind of object
* returned.
* @exception IOException if an I/O error occurs while
* getting the content.
* @exception UnknownServiceException if the protocol does not support
* the content type.
* @see java.net.ContentHandlerFactory#createContentHandler(java.lang.String)
* @see java.net.URLConnection#getContentType()
* @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
* @since JDK1.0
*/
public Object getContent() throws IOException {
return getContentHandler().getContent(this);
}
/**
* Returns an input stream that reads from this open connection.
*
* @return an input stream that reads from this open connection.
* @exception IOException if an I/O error occurs while
* creating the input stream.
* @exception UnknownServiceException if the protocol does not support
* input.
* @since JDK1.0
*/
public InputStream getInputStream() throws IOException {
throw new UnknownServiceException("protocol doesn't support input");
}
/**
* Returns an output stream that writes to this connection.
*
* @return an output stream that writes to this connection.
* @exception IOException if an I/O error occurs while
* creating the output stream.
* @exception UnknownServiceException if the protocol does not support
* output.
* @since JDK1.0
*/
public OutputStream getOutputStream() throws IOException {
throw new UnknownServiceException("protocol doesn't support output");
}
/**
* Returns a <code>String</code> representation of this URL connection.
*
* @return a string representation of this <code>URLConnection</code>.
* @since JDK1.0
*/
public String toString() {
return this.getClass().getName() + ":" + url;
}
/**
* Sets the value of the <code>doInput</code> field for this
* <code>URLConnection</code> to the specified value.
* <p>
* 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.
*
* @param value the new value.
* @see java.net.URLConnection#doInput
* @since JDK1.0
*/
public void setDoInput(boolean doinput) {
if (connected)
throw new IllegalAccessError("Already connected");
doInput = doinput;
}
/**
* Returns the value of this <code>URLConnection</code>'s
* <code>doInput</code> flag.
*
* @return the value of this <code>URLConnection</code>'s
* <code>doInput</code> flag.
* @see java.net.URLConnection#doInput
* @since JDK1.0
*/
public boolean getDoInput() {
return doInput;
}
/**
* Sets the value of the <code>doOutput</code> field for this
* <code>URLConnection</code> to the specified value.
* <p>
* 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.
*
* @param value the new value.
* @see java.net.URLConnection#doOutput
* @since JDK1.0
*/
public void setDoOutput(boolean dooutput) {
if (connected)
throw new IllegalAccessError("Already connected");
doOutput = dooutput;
}
/**
* Returns the value of this <code>URLConnection</code>'s
* <code>doOutput</code> flag.
*
* @return the value of this <code>URLConnection</code>'s
* <code>doOutput</code> flag.
* @see java.net.URLConnection#doOutput
* @since JDK1.0
*/
public boolean getDoOutput() {
return doOutput;
}
/**
* Set the value of the <code>allowUserInteraction</code> field of
* this <code>URLConnection</code>.
*
* @param allowuserinteraction the new value.
* @see java.net.URLConnection#allowUserInteraction
* @since JDK1.0
*/
public void setAllowUserInteraction(boolean allowuserinteraction) {
if (connected)
throw new IllegalAccessError("Already connected");
allowUserInteraction = allowuserinteraction;
}
/**
* Returns the value of the <code>allowUserInteraction</code> field for
* this object.
*
* @return the value of the <code>allowUserInteraction</code> field for
* this object.
* @see java.net.URLConnection#allowUserInteraction
* @since JDK1.0
*/
public boolean getAllowUserInteraction() {
return allowUserInteraction;
}
/**
* Sets the default value of the
* <code>allowUserInteraction</code> field for all future
* <code>URLConnection</code> objects to the specified value.
*
* @param defaultallowuserinteraction the new value.
* @see java.net.URLConnection#allowUserInteraction
* @since JDK1.0
*/
public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) {
defaultAllowUserInteraction = defaultallowuserinteraction;
}
/**
* Returns the default value of the <code>allowUserInteraction</code>
* field.
* <p>
* 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.
*
* @return the default value of the <code>allowUserInteraction</code>
* field.
* @see java.net.URLConnection#allowUserInteraction
* @since JDK1.0
*/
public static boolean getDefaultAllowUserInteraction() {
return defaultAllowUserInteraction;
}
/**
* Sets the value of the <code>useCaches</code> field of this
* <code>URLConnection</code> to the specified value.
* <p>
* 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.
*
* @see java.net.URLConnection#useCaches
* @since JDK1.0
*/
public void setUseCaches(boolean usecaches) {
if (connected)
throw new IllegalAccessError("Already connected");
useCaches = usecaches;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?