📄 urlconnection.java
字号:
defaultUseCaches = use; } /** * Sets the value of the named request property * * @param key The name of the property * @param value The value of the property * * @exception IllegalStateException If already connected * @exception NullPointerException If key is null * * @see URLConnection#getRequestProperty(String key) * @see URLConnection#addRequestProperty(String key, String value) * * @since 1.4 */ public void setRequestProperty(String key, String value) { if (connected) throw new IllegalStateException("Already connected"); if (key == null) throw new NullPointerException("key is null"); // Do nothing unless overridden by subclasses that support setting // header fields in the request. } /** * Adds a new request property by a key/value pair. * This method does not overwrite existing properties with the same key. * * @param key Key of the property to add * @param value Value of the Property to add * * @exception IllegalStateException If already connected * @exception NullPointerException If key is null * * @see URLConnection#getRequestProperty(String key) * @see URLConnection#setRequestProperty(String key, String value) * * @since 1.4 */ public void addRequestProperty(String key, String value) { if (connected) throw new IllegalStateException("Already connected"); if (key == null) throw new NullPointerException("key is null"); // Do nothing unless overridden by subclasses that support adding // header fields in the request. } /** * Returns the value of the named request property. * * @param key The name of the property * * @return Value of the property * * @exception IllegalStateException If already connected * * @see URLConnection#setRequestProperty(String key, String value) * @see URLConnection#addRequestProperty(String key, String value) */ public String getRequestProperty(String key) { if (connected) throw new IllegalStateException("Already connected"); // Overridden by subclasses that support reading header fields from the // request. return null; } /** * Returns an unmodifiable Map containing the request properties. * * @return The map of properties * * @exception IllegalStateException If already connected * * @since 1.4 */ public Map getRequestProperties() { if (connected) throw new IllegalStateException("Already connected"); // Overridden by subclasses that support reading header fields from the // request. return Collections.EMPTY_MAP; } /** * Sets the default value of a request property. This will be used * for all connections unless the value of the property is manually * overridden. * * @param key The request property name the default is being set for * @param value The value to set the default to * * @deprecated 1.3 The method setRequestProperty should be used instead. * This method does nothing now. * * @see URLConnection#setRequestProperty(String key, String value) */ public static void setDefaultRequestProperty(String key, String value) { // This method does nothing since JDK 1.3. } /** * Returns the default value of a request property. This will be used * for all connections unless the value of the property is manually * overridden. * * @param key The request property to return the default value of * * @return The value of the default property or null if not available * * @deprecated 1.3 The method getRequestProperty should be used instead. * This method does nothing now. * * @see URLConnection#getRequestProperty(String key) */ public static String getDefaultRequestProperty(String key) { // This method does nothing since JDK 1.3. return null; } /** * Sets the ContentHandlerFactory for an application. This can be called * once and only once. If it is called again, then an Error is thrown. * Unlike for other set factory methods, this one does not do a security * check prior to setting the factory. * * @param factory The ContentHandlerFactory for this application * * @exception Error If the factory has already been defined * @exception SecurityException If a security manager exists and its * checkSetFactory method doesn't allow the operation */ public static synchronized void setContentHandlerFactory(ContentHandlerFactory factory) { if (URLConnection.factory != null) throw new Error("ContentHandlerFactory already set"); // Throw an exception if an extant security mgr precludes // setting the factory. SecurityManager s = System.getSecurityManager(); if (s != null) s.checkSetFactory(); URLConnection.factory = factory; } /** * Returns the MIME type of a file based on the name of the file. This * works by searching for the file's extension in a list of file extensions * and returning the MIME type associated with it. If no type is found, * then a MIME type of "application/octet-stream" will be returned. * * @param filename The filename to determine the MIME type for * * @return The MIME type String * * @specnote public since JDK 1.4 */ public static String guessContentTypeFromName(String filename) { int dot = filename.lastIndexOf("."); if (dot != -1) { if (dot == filename.length()) return "application/octet-stream"; else filename = filename.substring(dot + 1); } String type = MimeTypes.getMimeTypeFromExtension(filename); if (type == null) return"application/octet-stream"; return type; } /** * Returns the MIME type of a stream based on the first few characters * at the beginning of the stream. This routine can be used to determine * the MIME type if a server is believed to be returning an incorrect * MIME type. This method returns "application/octet-stream" if it * cannot determine the MIME type. * <p> * NOTE: Overriding MIME types sent from the server can be obnoxious * to user's. See Internet Exploder 4 if you don't believe me. * * @param is The InputStream to determine the MIME type from * * @return The MIME type * * @exception IOException If an error occurs */ public static String guessContentTypeFromStream(InputStream is) throws IOException { is.mark(1024); // FIXME: Implement this. Use system mimetype informations (like "file"). is.reset(); return null; } /** * This method returns the <code>FileNameMap</code> object being used * to decode MIME types by file extension. * * @return The <code>FileNameMap</code>. * * @since 1.2 */ public static FileNameMap getFileNameMap() { return fileNameMap; } /** * This method sets the <code>FileNameMap</code> object being used * to decode MIME types by file extension. * * @param map The <code>FileNameMap</code>. * * @exception SecurityException If a security manager exists and its * checkSetFactory method doesn't allow the operation * * @since 1.2 */ public static void setFileNameMap(FileNameMap map) { // Throw an exception if an extant security manager precludes // setting the factory. SecurityManager s = System.getSecurityManager(); if (s != null) s.checkSetFactory(); fileNameMap = map; } private ContentHandler getContentHandler(String contentType) { // No content type so just handle it as the default. if (contentType == null || contentType.equals("")) return null; ContentHandler handler = null; // See if a handler has been cached for this content type. // For efficiency, if a content type has been searched for but not // found, it will be in the hash table but as the contentType String // instead of a ContentHandler. { Object cachedHandler; if ((cachedHandler = handlers.get(contentType)) != null) { if (cachedHandler instanceof ContentHandler) return (ContentHandler)cachedHandler; else return null; } } // If a non-default factory has been set, use it. if (factory != null) handler = factory.createContentHandler(contentType); // Now try default factory. Using this factory to instantiate built-in // content handlers is preferable if (handler == null) handler = defaultFactory.createContentHandler(contentType); // User-set factory has not returned a handler. Use the default search // algorithm. if (handler == null) { // Get the list of packages to check and append our default handler // to it, along with the JDK specified default as a last resort. // Except in very unusual environments the JDK specified one shouldn't // ever be needed (or available). String propVal = System.getProperty("java.content.handler.pkgs"); propVal = (propVal == null) ? "" : (propVal + "|"); propVal = propVal + "gnu.java.net.content|sun.net.www.content"; // Replace the '/' character in the content type with '.' and // all other non-alphabetic, non-numeric characters with '_'. char[] cArray = contentType.toCharArray(); for (int i = 0; i < cArray.length; i++) { if (cArray[i] == '/') cArray[i] = '.'; else if (! ((cArray[i] >= 'A' && cArray[i] <= 'Z') || (cArray[i] >= 'a' && cArray[i] <= 'z') || (cArray[i] >= '0' && cArray[i] <= '9'))) cArray[i] = '_'; } String contentClass = new String(cArray); // See if a class of this content type exists in any of the packages. StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|"); do { String facName = pkgPrefix.nextToken() + "." + contentClass; try { handler = (ContentHandler) Class.forName(facName).newInstance(); } catch (Exception e) { // Can't instantiate; handler still null, go on to next element. } } while ((handler == null || ! (handler instanceof ContentHandler)) && pkgPrefix.hasMoreTokens()); } // Update the hashtable with the new content handler. if (handler instanceof ContentHandler) { handlers.put(contentType, handler); return handler; } // For efficiency on subsequent searches, put a dummy entry in the hash // table for content types that don't have a non-default ContentHandler. handlers.put(contentType, contentType); return null; } // We don't put these in a static initializer, because it creates problems // with initializer co-dependency: SimpleDateFormat's constructors eventually // depend on URLConnection (via the java.text.*Symbols classes). private static synchronized void initializeDateFormats() { if (dateformats_initialized) return; Locale locale = new Locale("En", "Us", "Unix"); dateFormats = new SimpleDateFormat[3]; dateFormats[0] = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'", locale); dateFormats[1] = new SimpleDateFormat("EEEE, dd-MMM-yy hh:mm:ss 'GMT'", locale); dateFormats[2] = new SimpleDateFormat("EEE MMM d hh:mm:ss yyyy", locale); dateformats_initialized = true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -