📄 urlconnection.java
字号:
/* * @(#)URLConnection.java 1.98 03/08/14 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.net;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Hashtable;import java.text.DateFormat;import java.util.StringTokenizer;import java.util.Collections;import java.util.Map;import java.security.Permission;import java.security.AccessController;import sun.security.util.SecurityConstants;/** * The abstract class <code>URLConnection</code> 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 multistep process: * <p> * <center><table border=2 summary="Describes the process of creating a connection to a URL: openConnection() and connect() over time."> * <tr><th><code>openConnection()</code></th> * <th><code>connect()</code></th></tr> * <tr><td>Manipulate parameters that affect the connection to the remote * resource.</td> * <td>Interact with the resource; query header fields and * contents.</td></tr> * </table> * ----------------------------> * <br>time</center> * * <ol> * <li>The connection object is created by invoking the * <code>openConnection</code> method on a URL. * <li>The setup parameters and general request properties are manipulated. * <li>The actual connection to the remote object is made, using the * <code>connect</code> method. * <li>The remote object becomes available. The header fields and the contents * of the remote object can be accessed. * </ol> * <p> * The setup parameters are modified using the following methods: * <ul> * <li><code>setAllowUserInteraction</code> * <li><code>setDoInput</code> * <li><code>setDoOutput</code> * <li><code>setIfModifiedSince</code> * <li><code>setUseCaches</code> * </ul> * <p> * and the general request properties are modified using the method: * <ul> * <li><code>setRequestProperty</code> * </ul> * <p> * Default values for the <code>AllowUserInteraction</code> and * <code>UseCaches</code> parameters can be set using the methods * <code>setDefaultAllowUserInteraction</code> and * <code>setDefaultUseCaches</code>. * <p> * Each of the above <code>set</code> methods has a corresponding * <code>get</code> method to retrieve the value of the parameter or * general request property. The specific parameters and general * request properties that are applicable are protocol specific. * <p> * The following methods are used to access the header fields and * the contents after the connection is made to the remote object: * <ul> * <li><code>getContent</code> * <li><code>getHeaderField</code> * <li><code>getInputStream</code> * <li><code>getOutputStream</code> * </ul> * <p> * Certain header fields are accessed frequently. The methods: * <ul> * <li><code>getContentEncoding</code> * <li><code>getContentLength</code> * <li><code>getContentType</code> * <li><code>getDate</code> * <li><code>getExpiration</code> * <li><code>getLastModifed</code> * </ul> * <p> * provide convenient access to these fields. The * <code>getContentType</code> method is used by the * <code>getContent</code> method to determine the type of the remote * object; subclasses may find it convenient to override the * <code>getContentType</code> method. * <p> * In the common case, all of the pre-connection parameters and * general request properties can be ignored: the pre-connection * parameters and request properties default to sensible values. For * most clients of this interface, there are only two interesting * methods: <code>getInputStream</code> and <code>getContent</code>, * which are mirrored in the <code>URL</code> class by convenience methods. * <p> * More information on the request properties and header fields of * an <code>http</code> connection can be found at: * <blockquote><pre> * <a href="http://www.ietf.org/rfc/rfc2068.txt">http://www.ietf.org/rfc/rfc2068.txt</a> * </pre></blockquote> * * Note about <code>fileNameMap</code>: In versions prior to JDK 1.1.6, * field <code>fileNameMap</code> of <code>URLConnection</code> was public. * In JDK 1.1.6 and later, <code>fileNameMap</code> is private; accessor * and mutator methods {@link #getFileNameMap() getFileNameMap} and * {@link #setFileNameMap(java.net.FileNameMap) setFileNameMap} are added * to access it. This change is also described on the <a href= * "http://java.sun.com/products/jdk/1.2/compatibility.html#incompatibilities1.2"> * Compatibility</a> page. * * Calling the <tt>close()</tt> methods on the <tt>InputStream</tt> or <tt>OutputStream</tt> of an * <tt>URLConnection</tt> after a request may free network resources associated with this * instance, unless particular protocol specifications specify different behaviours * for it. * * @author James Gosling * @version 1.75, 05/03/00 * @see java.net.URL#openConnection() * @see java.net.URLConnection#connect() * @see java.net.URLConnection#getContent() * @see java.net.URLConnection#getContentEncoding() * @see java.net.URLConnection#getContentLength() * @see java.net.URLConnection#getContentType() * @see java.net.URLConnection#getDate() * @see java.net.URLConnection#getExpiration() * @see java.net.URLConnection#getHeaderField(int) * @see java.net.URLConnection#getHeaderField(java.lang.String) * @see java.net.URLConnection#getInputStream() * @see java.net.URLConnection#getLastModified() * @see java.net.URLConnection#getOutputStream() * @see java.net.URLConnection#setAllowUserInteraction(boolean) * @see java.net.URLConnection#setDefaultUseCaches(boolean) * @see java.net.URLConnection#setDoInput(boolean) * @see java.net.URLConnection#setDoOutput(boolean) * @see java.net.URLConnection#setIfModifiedSince(long) * @see java.net.URLConnection#setRequestProperty(java.lang.String, java.lang.String) * @see java.net.URLConnection#setUseCaches(boolean) * @since JDK1.0 */public abstract class URLConnection { /** * The URL represents the remote object on the World Wide Web to * which this connection is opened. * <p> * The value of this field can be accessed by the * <code>getURL</code> method. * <p> * The default value of this variable is the value of the URL * argument in the <code>URLConnection</code> constructor. * * @see java.net.URLConnection#getURL() * @see java.net.URLConnection#url */ protected URL url; /** * This variable is set by the <code>setDoInput</code> method. Its * value is returned by the <code>getDoInput</code> method. * <p> * A URL connection can be used for input and/or output. Setting the * <code>doInput</code> flag to <code>true</code> indicates that * the application intends to read data from the URL connection. * <p> * The default value of this field is <code>true</code>. * * @see java.net.URLConnection#getDoInput() * @see java.net.URLConnection#setDoInput(boolean) */ protected boolean doInput = true; /** * This variable is set by the <code>setDoOutput</code> method. Its * value is returned by the <code>getDoOutput</code> method. * <p> * A URL connection can be used for input and/or output. Setting the * <code>doOutput</code> flag to <code>true</code> indicates * that the application intends to write data to the URL connection. * <p> * The default value of this field is <code>false</code>. * * @see java.net.URLConnection#getDoOutput() * @see java.net.URLConnection#setDoOutput(boolean) */ protected boolean doOutput = false; private static boolean defaultAllowUserInteraction = false; /** * If <code>true</code>, this <code>URL</code> is being examined in * a context in which it makes sense to allow user interactions such * as popping up an authentication dialog. If <code>false</code>, * then no user interaction is allowed. * <p> * The value of this field can be set by the * <code>setAllowUserInteraction</code> method. * Its value is returned by the * <code>getAllowUserInteraction</code> method. * Its default value is the value of the argument in the last invocation * of the <code>setDefaultAllowUserInteraction</code> method. * * @see java.net.URLConnection#getAllowUserInteraction() * @see java.net.URLConnection#setAllowUserInteraction(boolean) * @see java.net.URLConnection#setDefaultAllowUserInteraction(boolean) */ protected boolean allowUserInteraction = defaultAllowUserInteraction; private static boolean defaultUseCaches = true; /** * If <code>true</code>, the protocol is allowed to use caching * whenever it can. If <code>false</code>, the protocol must always * try to get a fresh copy of the object. * <p> * This field is set by the <code>setUseCaches</code> method. Its * value is returned by the <code>getUseCaches</code> method. * <p> * Its default value is the value given in the last invocation of the * <code>setDefaultUseCaches</code> method. * * @see java.net.URLConnection#setUseCaches(boolean) * @see java.net.URLConnection#getUseCaches() * @see java.net.URLConnection#setDefaultUseCaches(boolean) */ protected boolean useCaches = defaultUseCaches; /** * Some protocols support skipping the fetching of the object unless * the object has been modified more recently than a certain time. * <p> * A nonzero value gives a time as the number of milliseconds since * January 1, 1970, GMT. The object is fetched only if it has been * modified more recently than that time. * <p> * This variable is set by the <code>setIfModifiedSince</code> * method. Its value is returned by the * <code>getIfModifiedSince</code> method. * <p> * The default value of this field is <code>0</code>, indicating * that the fetching must always occur. * * @see java.net.URLConnection#getIfModifiedSince() * @see java.net.URLConnection#setIfModifiedSince(long) */ protected long ifModifiedSince = 0; /** * If <code>false</code>, this connection object has not created a * communications link to the specified URL. If <code>true</code>, * the communications link has been established. */ protected boolean connected = false; /** * @since JDK1.1 */ private static FileNameMap fileNameMap; /** * @since 1.2.2 */ private static boolean fileNameMapLoaded = false; /** * Loads filename map (a mimetable) from a data file. It will * first try to load the user-specific table, defined * by "content.types.user.table" property. If that fails, * it tries to load the default built-in table at * lib/content-types.properties under java home. * * @return the FileNameMap * @since 1.2 * @see #setFileNameMap(java.net.FileNameMap) */ public static synchronized FileNameMap getFileNameMap() { if ((fileNameMap == null) && !fileNameMapLoaded) { fileNameMap = sun.net.www.MimeTable.loadTable(); fileNameMapLoaded = true; } return new FileNameMap() { private FileNameMap map = fileNameMap; public String getContentTypeFor(String fileName) { return map.getContentTypeFor(fileName); } }; } /** * Sets the FileNameMap. * <p> * If there is a security manager, this method first calls * the security manager's <code>checkSetFactory</code> method * to ensure the operation is allowed. * This could result in a SecurityException. * * @param map the FileNameMap to be set * @exception SecurityException if a security manager exists and its * <code>checkSetFactory</code> method doesn't allow the operation. * @see SecurityManager#checkSetFactory * @see #getFileNameMap() * @since 1.2 */ public static void setFileNameMap(FileNameMap map) { SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkSetFactory(); fileNameMap = map; } /** * Opens a communications link to the resource referenced by this * URL, if such a connection has not already been established. * <p> * If the <code>connect</code> method is called when the connection * has already been opened (indicated by the <code>connected</code> * field having the value <code>true</code>), the call is ignored. * <p> * URLConnection objects go through two phases: first they are * created, then they are connected. After being created, and * before being connected, various options can be specified * (e.g., doInput and UseCaches). After connecting, it is an * error to try to set them. Operations that depend on being * connected, like getContentLength, will implicitly perform the * connection, if necessary. * * @exception IOException if an I/O error occurs while opening the * connection. * @see java.net.URLConnection#connected */ abstract public void connect() throws IOException; /** * Constructs a URL connection to the specified URL. A connection to * the object referenced by the URL is not created. * * @param url the specified URL. */ protected URLConnection(URL url) { this.url = url; } /** * Returns the value of this <code>URLConnection</code>'s <code>URL</code> * field. * * @return the value of this <code>URLConnection</code>'s <code>URL</code> * field. * @see java.net.URLConnection#url */ public URL getURL() { return url; } /** * Returns the value of the <code>content-length</code> header field. * * @return the content length of the resource that this connection's URL * references, or <code>-1</code> if the content length is * not known. */ public int getContentLength() { return getHeaderFieldInt("content-length", -1); } /** * Returns the value of the <code>content-type</code> header field. * * @return the content type of the resource that the URL references, * or <code>null</code> if not known. * @see java.net.URLConnection#getHeaderField(java.lang.String) */ public String getContentType() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -