@SuppressWarnings("unchecked") public <T> T newProxy(ProxyTarget<T> proxyTarget)throws UnsupportedEncodingException { // 默认方法 List<DefaultMethodInvoker> defaultProxyInvokerList = new LinkedList<>();
publicURL(URL context, String spec, URLStreamHandler handler)throws MalformedURLException{ String original = spec; int i, limit, c; int start = 0; String newProtocol = null; boolean aRef=false; boolean isRelative = false;
// Check for permission to specify a handler if (handler != null) { @SuppressWarnings("removal") SecurityManager sm = System.getSecurityManager(); if (sm != null) { checkSpecifyHandler(sm); } }
if (spec.regionMatches(true, start, "url:", 0, 4)) { start += 4; } if (start < spec.length() && spec.charAt(start) == '#') { /* we're assuming this is a ref relative to the context URL. * This means protocols cannot start w/ '#', but we must parse * ref URL's like: "hello:there" w/ a ':' in them. */ aRef=true; } for (i = start ; !aRef && (i < limit) && ((c = spec.charAt(i)) != '/') ; i++) { if (c == ':') { String s = toLowerCase(spec.substring(start, i)); if (isValidProtocol(s)) { newProtocol = s; start = i + 1; } break; } }
// Only use our context if the protocols match. protocol = newProtocol; if ((context != null) && ((newProtocol == null) || newProtocol.equalsIgnoreCase(context.protocol))) { // inherit the protocol handler from the context // if not specified to the constructor if (handler == null) { handler = context.handler; }
// If the context is a hierarchical URL scheme and the spec // contains a matching scheme then maintain backwards // compatibility and treat it as if the spec didn't contain // the scheme; see 5.2.3 of RFC2396 if (context.path != null && context.path.startsWith("/")) newProtocol = null;
if (protocol == null) { thrownew MalformedURLException("no protocol: "+original); }
// Get the protocol handler if not specified or the protocol // of the context could not be used if (handler == null && (handler = getURLStreamHandler(protocol)) == null) { thrownew MalformedURLException("unknown protocol: "+protocol); } this.handler = handler;
i = spec.indexOf('#', start); if (i >= 0) { ref = spec.substring(i + 1, limit); limit = i; }
/* * Handle special case inheritance of query and fragment * implied by RFC2396 section 5.2.2. */ if (isRelative && start == limit) { query = context.query; if (ref == null) { ref = context.ref; } } handler.parseURL(this, spec, start, limit); } catch(MalformedURLException e) { throw e; } catch(Exception e) { MalformedURLException exception = new MalformedURLException(e.getMessage()); exception.initCause(e); throw exception; } }
public URLConnection openConnection()throws java.io.IOException { return handler.openConnection(this); }
不过这里只是创建了一个连接实例,真正进行连接还需要主动调用connect()
Returns a URLConnection instance that represents a connection to the remote object referred to by the URL. A new instance of URLConnection is created every time when invoking the URLStreamHandler.openConnection(URL) method of the protocol handler for this URL. It should be noted that a URLConnection instance does not establish the actual network connection on creation. This will happen only when calling URLConnection.connect(). If for the URL’s protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.
publicvoidopenServer(String server, int port)throws IOException, UnknownHostException { if (serverSocket != null) closeServer(); serverSocket = doConnect (server, port); try { serverOutput = new PrintStream(new BufferedOutputStream(serverSocket.getOutputStream()), true, encoding); } catch (UnsupportedEncodingException e) { thrownew InternalError(encoding +"encoding not found", e); } serverInput = new BufferedInputStream(serverSocket.getInputStream()); }
protected Socket doConnect(String server, int port)throws IOException, UnknownHostException { Socket s; if (proxy != null) { if (proxy.type() == Proxy.Type.SOCKS) { s = AccessController.doPrivileged( new PrivilegedAction<>() { public Socket run(){ returnnew Socket(proxy); } }); } elseif (proxy.type() == Proxy.Type.DIRECT) { s = createSocket(); } else { // Still connecting through a proxy // server & port will be the proxy address and port s = new Socket(Proxy.NO_PROXY); } } else { s = createSocket(); }
// Instance specific timeouts do have priority, that means // connectTimeout & readTimeout (-1 means not set) // Then global default timeouts // Then no timeout. if (connectTimeout >= 0) { s.connect(new InetSocketAddress(server, port), connectTimeout); } else { if (defaultConnectTimeout > 0) { s.connect(new InetSocketAddress(server, port), defaultConnectTimeout); } else { s.connect(new InetSocketAddress(server, port)); } } if (readTimeout >= 0) s.setSoTimeout(readTimeout); elseif (defaultSoTimeout > 0) { s.setSoTimeout(defaultSoTimeout); } return s; }
publicvoidsetRequestMethod(String method)throws ProtocolException { if (connected) { thrownew ProtocolException("Can't reset method: already connected"); } // This restriction will prevent people from using this class to // experiment w/ new HTTP methods using java. But it should // be placed for security - the request String could be // arbitrarily long.
for (int i = 0; i < methods.length; i++) { if (methods[i].equals(method)) { if (method.equals("TRACE")) { SecurityManager s = System.getSecurityManager(); if (s != null) { s.checkPermission(new NetPermission("allowHttpTrace")); } } this.method = method; return; } } thrownew ProtocolException("Invalid HTTP method: " + method); }