创建HttpConnection

@param urlStr URL
@param proxy 代理，无代理传{@code null}
@return HttpConnection
768220
public static HttpConnection create(String urlStr, Proxy proxy) {
		return create(URLUtil.toUrlForHttp(urlStr), proxy);
	}
566050
private void connectTunnel(int connectTimeout, int readTimeout, int writeTimeout, Call call,
      EventListener eventListener) throws IOException {
    Request tunnelRequest = createTunnelRequest();
    HttpUrl url = tunnelRequest.url();
    for (int i = 0; i < MAX_TUNNEL_ATTEMPTS; i++) {
      connectSocket(connectTimeout, readTimeout, call, eventListener);
      tunnelRequest = createTunnel(readTimeout, writeTimeout, tunnelRequest, url);

      if (tunnelRequest == null) break; // Tunnel successfully created.

      // The proxy decided to close the connection after an auth challenge. We need to create a new
      // connection, but this time with the auth credentials.
      closeQuietly(rawSocket);
      rawSocket = null;
      sink = null;
      source = null;
      eventListener.connectEnd(call, route.socketAddress(), route.proxy(), null);
    }
  }
536751
public static String doPost(String url, String ctype, byte[] content, int connectTimeout,
                                int readTimeout, String proxyHost, int proxyPort) throws IOException {
        HttpURLConnection conn = null;
        OutputStream out = null;
        String rsp = null;
        try {
            try {
                conn = null;
                if (!StringUtils.isEmpty(proxyHost)) {
                    conn = getConnection(new URL(url), METHOD_POST, ctype, proxyHost, proxyPort);
                } else {
                    conn = getConnection(new URL(url), METHOD_POST, ctype);
                }
                conn.setConnectTimeout(connectTimeout);
                conn.setReadTimeout(readTimeout);
            } catch (IOException e) {
                Map<String, String> map = getParamsFromUrl(url);
                AlipayLogger.logCommError(e, url, map.get("app_key"), map.get("method"), content);
                throw e;
            }
            try {
                out = conn.getOutputStream();
                out.write(content);
                rsp = getResponseAsString(conn);
            } catch (IOException e) {
                Map<String, String> map = getParamsFromUrl(url);
                AlipayLogger.logCommError(e, conn, map.get("app_key"), map.get("method"), content);
                throw e;
            }

        } finally {
            if (out != null) {
                out.close();
            }
            if (conn != null) {
                conn.disconnect();

            }
        }

        return rsp;
    }
461044
public T hostFromUrl(String urlStr) {
        if (StringUtils.isNotBlank(urlStr)) {
            try {
                URL url = new URL(urlStr);
                defaultHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
            } catch (MalformedURLException e) {
                throw new IllegalArgumentException("Cannot parse the url " + urlStr, e);
            }
        } else {
            defaultHost = null;
        }
        return self();
    }
1062818
static Proxy parseProxySettings(String spec) {

        try {
            if (spec == null || spec.length() == 0) {
                return null;
            }
            return new Proxy(spec);
        } catch (URISyntaxException e) {
            return null;
        }
    }
840049
public Proxy setHttpProxy(String httpProxy) {
    verifyProxyTypeCompatibility(ProxyType.MANUAL);
    this.proxyType = ProxyType.MANUAL;
    this.httpProxy = httpProxy;
    return this;
  }
1007972
public URI proxy(URI uri) {
        Proxy conf;
        String scheme;

        scheme = uri.getScheme();
        conf = proxies.get(scheme);
        if (conf == null || conf.excludes(uri)) {
            return null;
        }
        try {
            return new URI(scheme, null, conf.host, conf.port, null, null, null);
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        }
    }
196931
private HttpURLConnection openConnection(URL url) throws IOException {
        URLConnection urlConnection = url.openConnection();
        if (!(urlConnection instanceof HttpURLConnection)) {
            throw new IllegalStateException("Connection should be an HttpURLConnection");
        }
        return (HttpURLConnection) urlConnection;
    }
265425
@SuppressWarnings("unused")
    public URLConnection openConnection(URL url, Proxy proxy) throws IOException {
        try {
            return (URLConnection) _openConnectionProxy.invoke(getInstance(), new Object[] { url, proxy });
        } catch (Exception e) {
            FFDCFilter.processException(e, getClass().getName(), "openConnection", new Object[] { url, proxy });
            return null;
        }
    }
1021471
public void setHTTPProxy(String PROXYNAME, int PROXYPORT) {
		if (PROXYNAME == null) {
			this.proxy_to_use = null;
		}
		else {
			this.proxy_to_use = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXYNAME, PROXYPORT));
		}
	}
561194
public static URLConnection open(URL url) throws IOException {
        final ProxyConfiguration p = get();
        
        URLConnection con;
        if(p==null) {
            con = url.openConnection();
        } else {
            Proxy proxy = p.createProxy(url.getHost());
            con = url.openConnection(proxy);
            if(p.getUserName()!=null) {
                // Add an authenticator which provides the credentials for proxy authentication
                Authenticator.setDefault(p.authenticator);
                p.jenkins48775workaround(proxy, url);
            }
        }
        
        if(DEFAULT_CONNECT_TIMEOUT_MILLIS > 0) {
            con.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS);
        }
        
        if (JenkinsJVM.isJenkinsJVM()) { // this code may run on a slave
            decorate(con);
        }

        return con;
    }
1102588
def get_proxy(self, proxystr=''):
        
        if not proxystr:
            proxystr = os.environ.get('HTTP_PROXY', '')
        if proxystr:
            if '@' in proxystr:
                user_password, server_port = proxystr.split('@', 1)
                if ':' in user_password:
                    user, password = user_password.split(':', 1)
                else:
                    user = user_password
                    prompt = 'Password for %s@%s: ' % (user, server_port)
                    password = urllib.quote(getpass.getpass(prompt))
                return '%s:%s@%s' % (user, password, server_port)
            else:
                return proxystr
        else:
            return None
102232
@Override
	public HttpConnection getConnectionWithTimeout(
			HostConfiguration hostConfiguration, long timeout) {

		if (httpConnection == null) {
			httpConnection = new ZapHttpConnection(hostConfiguration);
			httpConnection.setHttpConnectionManager(this);
			httpConnection.getParams().setDefaults(this.getParams());
		}
		
		return super.getConnectionWithTimeout(hostConfiguration, timeout);
	}
890168
protected HttpTunnel newHttpTunnel(HttpRequest request, HttpResponse response, InetAddress iaddr, int port, int timeoutMS) throws IOException
    {
        try
        {
            Socket socket=null;
            InputStream in=null;
            
            String chained_proxy_host=System.getProperty("http.proxyHost");
            if (chained_proxy_host==null)
            {
                socket= new Socket(iaddr, port);
                socket.setSoTimeout(timeoutMS);
                socket.setTcpNoDelay(true);
            }
            else
            {
                int chained_proxy_port = Integer.getInteger("http.proxyPort", 8888).intValue();
                
                Socket chain_socket= new Socket(chained_proxy_host, chained_proxy_port);
                chain_socket.setSoTimeout(timeoutMS);
                chain_socket.setTcpNoDelay(true);
                if (log.isDebugEnabled()) log.debug("chain proxy socket="+chain_socket);
                
                LineInput line_in = new LineInput(chain_socket.getInputStream());
                byte[] connect= request.toString().getBytes(org.browsermob.proxy.jetty.util.StringUtil.__ISO_8859_1);
                chain_socket.getOutputStream().write(connect);
                
                String chain_response_line = line_in.readLine();
                HttpFields chain_response = new HttpFields();
                chain_response.read(line_in);
                
                // decode response
                int space0 = chain_response_line.indexOf(' ');
                if (space0>0 && space0+1<chain_response_line.length())
                {
                    int space1 = chain_response_line.indexOf(' ',space0+1);
                    
                    if (space1>space0)
                    {
                        int code=Integer.parseInt(chain_response_line.substring(space0+1,space1));
                        
                        if (code>=200 && code<300)
                        {
                            socket=chain_socket;
                            in=line_in;
                        }
                        else
                        {
                            Enumeration iter = chain_response.getFieldNames();
                            while (iter.hasMoreElements())
                            {
                                String name=(String)iter.nextElement();
                                if (!_DontProxyHeaders.containsKey(name))
                                {
                                    Enumeration values = chain_response.getValues(name);
                                    while(values.hasMoreElements())
                                    {
                                        String value=(String)values.nextElement();
                                        response.setField(name, value);
                                    }
                                }
                            }
                            response.sendError(code);
                            if (!chain_socket.isClosed())
                                chain_socket.close();
                        }
                    }
                }
            }
            
            if (socket==null)
                return null;
            HttpTunnel tunnel=new HttpTunnel(socket,in,null);
            return tunnel;
        }
        catch(IOException e)
        {
            log.debug(e);
            response.sendError(HttpResponse.__400_Bad_Request);
            return null;
        }
    }
334730
private static HttpURLConnection open(URL url) throws IOException {
        HttpURLConnection c = (HttpURLConnection)url.openConnection();
        c.setReadTimeout(TIMEOUT);
        c.setConnectTimeout(TIMEOUT);
        return c;
    }
423212
public static String post(String url, Map<String, String> queryParas, String data, Map<String, String> headers) {
		HttpURLConnection conn = null;
		try {
			conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers);
			conn.connect();
			
			OutputStream out = conn.getOutputStream();
			out.write(data != null ? data.getBytes(CHARSET) : null);
			out.flush();
			out.close();
			
			return readResponseString(conn);
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
		finally {
			if (conn != null) {
				conn.disconnect();
			}
		}
	}
1175897
private static InetSocketAddress overrideProxy(String proxyHostPort) {
    if (proxyHostPort == null) {
      return null;
    }

    String[] parts = proxyHostPort.split(":", 2);
    int port = 80;
    if (parts.length > 1) {
      port = Integer.parseInt(parts[1]);
    }
    log.warning(
        "Detected GRPC_PROXY_EXP and will honor it, but this feature will "
            + "be removed in a future release. Use the JVM flags "
            + "\"-Dhttps.proxyHost=HOST -Dhttps.proxyPort=PORT\" to set the https proxy for "
            + "this JVM.");
    return new InetSocketAddress(parts[0], port);
  }