?? connectionmanager.java
字號:
public void setCookieProcessingEnabled (boolean enable) { if (enable) mCookieJar = (null == mCookieJar) ? new Hashtable () : mCookieJar; else mCookieJar = null; } /** * Adds a cookie to the cookie jar. * @param cookie The cookie to add. * @param domain The domain to use in case the cookie has no domain attribute. */ public void setCookie (Cookie cookie, String domain) { String path; Vector cookies; Cookie probe; boolean found; // flag if a cookie with current name is already there if (null != cookie.getDomain ()) domain = cookie.getDomain (); path = cookie.getPath (); if (null == mCookieJar) mCookieJar = new Hashtable (); // turn on cookie processing cookies = (Vector)mCookieJar.get (domain); if (null != cookies) { found = false; for (int j = 0; j < cookies.size (); j++) { probe = (Cookie)cookies.elementAt (j); if (probe.getName ().equalsIgnoreCase (cookie.getName ())) { // we keep paths sorted most specific to least if (probe.getPath ().equals (path)) { cookies.setElementAt (cookie, j); // replace found = true; // cookie found, set flag break; } else if (path.startsWith (probe.getPath ())) { cookies.insertElementAt (cookie, j); found = true; // cookie found, set flag break; } } } if (!found) // there's no cookie with the current name, therefore it's added // at the end of the list (faster then inserting at the front) cookies.addElement (cookie); } else { // new cookie list needed cookies = new Vector (); cookies.addElement (cookie); mCookieJar.put (domain, cookies); } } /** * Get the monitoring object, if any. * @return Returns the monitor, or null if none has been assigned. */ public ConnectionMonitor getMonitor () { return (mMonitor); } /** * Set the monitoring object. * @param monitor The monitor to set. */ public void setMonitor (ConnectionMonitor monitor) { mMonitor = monitor; } /** * Predicate to determine if url redirection processing is currently enabled. * @return <code>true</code> if redirection is being processed manually. * @see #setRedirectionProcessingEnabled */ public boolean getRedirectionProcessingEnabled () { return (mRedirectionProcessingEnabled); } /** * Enables or disables manual redirection handling. * Normally the <code>HttpURLConnection</code> follows redirections * (HTTP response code 3xx) automatically if the * <code>followRedirects</code> property is <code>true</code>. * With this flag set the <code>ConnectionMonitor</code> performs the * redirection processing; The advantage being that cookies (if enabled) * are passed in subsequent requests. * @param enabled The new state of the redirectionProcessingEnabled property. */ public void setRedirectionProcessingEnabled (boolean enabled) { mRedirectionProcessingEnabled = enabled; } /** * Get the Location field if any. * @param http The connection to get the location from. */ protected String getLocation (HttpURLConnection http) { String key; String value; String ret; ret = null; for (int i = 0; ((null == ret) && (null != (value = http.getHeaderField (i)))); i++) if ((null != (key = http.getHeaderFieldKey (i))) && (key.equalsIgnoreCase ("Location"))) ret = value; return (ret); } /** * Opens a connection using the given url. * @param url The url to open. * @return The connection. * @exception ParserException if an i/o exception occurs accessing the url. */ public URLConnection openConnection (URL url) throws ParserException { boolean repeat; int repeated; Properties sysprops; Hashtable properties; Enumeration enumeration; String key; String value; String set = null; // old proxySet value String host = null; // old proxyHost value String port = null; // old proxyPort value String host2 = null; // old http.proxyHost value String port2 = null; // old http.proxyPort value HttpURLConnection http; String auth; String encoded; int code; String uri; URLConnection ret; repeated = 0; do { repeat = false; try { try { // set up for proxy if ((null != getProxyHost ()) && (0 != getProxyPort ())) { sysprops = System.getProperties (); set = (String)sysprops.put ("proxySet", "true"); host = (String)sysprops.put ("proxyHost", getProxyHost ()); port = (String)sysprops.put ("proxyPort", Integer.toString (getProxyPort ())); // see http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html host2 = (String)sysprops.put ("http.proxyHost", getProxyHost ()); port2 = (String)sysprops.put ("http.proxyPort", Integer.toString (getProxyPort ())); System.setProperties (sysprops); } // open the connection... but don't connect yet ret = url.openConnection (); if (ret instanceof HttpURLConnection) { http = (HttpURLConnection)ret; if (getRedirectionProcessingEnabled ()) http.setInstanceFollowRedirects (false); // set the fixed request properties properties = getRequestProperties (); if (null != properties) for (enumeration = properties.keys (); enumeration.hasMoreElements ();) { key = (String)enumeration.nextElement (); value = (String)properties.get (key); ret.setRequestProperty (key, value); } // set the proxy name and password if ((null != getProxyUser ()) && (null != getProxyPassword ())) { auth = getProxyUser () + ":" + getProxyPassword (); encoded = encode (auth.getBytes("ISO-8859-1")); ret.setRequestProperty ("Proxy-Authorization", encoded); } // set the URL name and password if ((null != getUser ()) && (null != getPassword ())) { auth = getUser () + ":" + getPassword (); encoded = encode (auth.getBytes("ISO-8859-1")); ret.setRequestProperty ("Authorization", "Basic " + encoded); } if (getCookieProcessingEnabled ()) // set the cookies based on the url addCookies (ret); if (null != getMonitor ()) getMonitor ().preConnect (http); } else http = null; try { ret.connect (); if (null != http) { if (null != getMonitor ()) getMonitor ().postConnect (http); if (getCookieProcessingEnabled ()) parseCookies (ret); code = http.getResponseCode (); if ((3 == (code / 100)) && (repeated < 20)) if (null != (uri = getLocation (http))) { url = new URL (uri); repeat = true; repeated++; } } } catch (UnknownHostException uhe) { int message = (int)(Math.random () * FOUR_OH_FOUR.length); throw new ParserException (FOUR_OH_FOUR[message], uhe); } catch (IOException ioe) { throw new ParserException (ioe.getMessage (), ioe); } } finally { if ((null != getProxyHost ()) && (0 != getProxyPort ())) { sysprops = System.getProperties (); if (null != set) sysprops.put ("proxySet", set); else sysprops.remove ("proxySet"); if (null != host) sysprops.put ("proxyHost", host); else sysprops.remove ("proxyHost"); if (null != port) sysprops.put ("proxyPort", port); else sysprops.remove ("proxyPort"); if (null != host2) sysprops.put ("http.proxyHost", host2); else sysprops.remove ("http.proxyHost"); if (null != port2) sysprops.put ("http.proxyPort", port2); else sysprops.remove ("http.proxyPort"); System.setProperties (sysprops); } } } catch (IOException ioe) { String msg = "Error in opening a connection to " + url.toExternalForm (); ParserException ex = new ParserException (msg, ioe); throw ex; } } while (repeat); return (ret); } /** * Encodes a byte array into BASE64 in accordance with * <a href="http://www.faqs.org/rfcs/rfc2045.html">RFC 2045</a>. * @param array The bytes to convert. * @return A BASE64 encoded string. */ public final static String encode (byte[] array) { int last; // last byte int count; // character count int separators; // line separator count int length; // length of returned string char[] encoded; // encoded characters int left; // bytes left int end; int block; // encoding buffer int r; // shift count int n; // byte to encode int index; // index into output array String ret; if ((null != array) && (0 != array.length)) { last = array.length - 1; count = (last / 3 + 1) << 2; separators = (count - 1) / 76; length = count + separators; encoded = new char[length]; index = 0; separators = 0; for (int i = 0; i <= last; i += 3) { left = last - i; end = (left > 1 ? 2 : left); // collect 1 to 3 bytes to encode block = 0; r = 16; for (int j = 0; j <= end; j++) { n = array[i + j]; block += (n < 0 ? n + 256 : n) << r; r -= 8; } // encode into 2-4 chars padding with '=' if no data left encoded[index++] = BASE64_CHAR_TABLE[(block >>> 18) & 0x3f]; encoded[index++] = BASE64_CHAR_TABLE[(block >>> 12) & 0x3f]; encoded[index++] = left > 0 ? BASE64_CHAR_TABLE[(block >>> 6) & 0x3f] : '='; encoded[index++] = left > 1 ? BASE64_CHAR_TABLE[block & 0x3f] : '='; if ((0 == (index - separators) % 76) && (index < length)) { encoded[index++] = '\n'; separators += 1; } } ret = new String (encoded); } else ret = ""; return (ret); } /** * Turn spaces into %20. * ToDo: make this more generic * (see RFE #1010593 provide URL encoding/decoding utilities). * @param url The url containing spaces. * @return The URL with spaces as %20 sequences. */ public String fixSpaces (String url) { int index; int length; char ch; StringBuffer buffer; index = url.indexOf (' '); if (-1 != index) { length = url.length (); buffer = new StringBuffer (length * 3); buffer.append (url.substring (0, index)); for (int i = index; i < length; i++) { ch = url.charAt (i); if (ch==' ') buffer.append ("%20"); else buffer.append (ch); } url = buffer.toString (); } return (url); } /** * Opens a connection based on a given string. * The string is either a file, in which case <code>file://localhost</code> * is prepended to a canonical path derived from the string, or a url that * begins with one of the known protocol strings, i.e. <code>http://</code>. * Embedded spaces are silently converted to %20 sequences. * @param string The name of a file or a url. * @return The connection. * @exception ParserException if the string is not a valid url or file. */ public URLConnection openConnection (String string)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -