private HttpClient getClient() { if (client == null) { HttpClientBuilder clientBuilder = HttpClients.custom(); RequestConfig.Builder configBuilder = RequestConfig.custom(); if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) { isUsingProxy = true; InetSocketAddress adr = (InetSocketAddress) proxy.address(); clientBuilder.setProxy(new HttpHost(adr.getHostName(), adr.getPort())); } if (timeout != null) { configBuilder.setConnectTimeout(timeout.intValue()); } if (readTimeout != null) { configBuilder.setSocketTimeout(readTimeout.intValue()); } if (followRedirects != null) { configBuilder.setRedirectsEnabled(followRedirects.booleanValue()); } if (hostnameverifier != null) { SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(getSSLContext(), hostnameverifier); clientBuilder.setSSLSocketFactory(sslConnectionFactory); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnectionFactory) .register("http", PlainConnectionSocketFactory.INSTANCE) .build(); clientBuilder.setConnectionManager(new BasicHttpClientConnectionManager(registry)); } clientBuilder.setDefaultRequestConfig(configBuilder.build()); client = clientBuilder.build(); } return client; }
/** * Configure the proxy with the required credential if needed * * @param httpClientBuilder * @param credsProvider * @param url * @return * @throws java.net.MalformedURLException */ private HttpClientBuilder configureProxy( HttpClientBuilder httpClientBuilder, CredentialsProvider credsProvider, String url) throws DSSException { try { if (proxyPreferenceManager == null) { return httpClientBuilder; } final String protocol = new URL(url).getProtocol(); final boolean proxyHTTPS = Protocol.isHttps(protocol) && proxyPreferenceManager.isHttpsEnabled(); final boolean proxyHTTP = Protocol.isHttp(protocol) && proxyPreferenceManager.isHttpEnabled(); if (!proxyHTTPS && !proxyHTTP) { return httpClientBuilder; } String proxyHost = null; int proxyPort = 0; String proxyUser = null; String proxyPassword = null; if (proxyHTTPS) { LOG.debug("Use proxy https parameters"); final Long port = proxyPreferenceManager.getHttpsPort(); proxyPort = port != null ? port.intValue() : 0; proxyHost = proxyPreferenceManager.getHttpsHost(); proxyUser = proxyPreferenceManager.getHttpsUser(); proxyPassword = proxyPreferenceManager.getHttpsPassword(); } else // noinspection ConstantConditions if (proxyHTTP) { LOG.debug("Use proxy http parameters"); final Long port = proxyPreferenceManager.getHttpPort(); proxyPort = port != null ? port.intValue() : 0; proxyHost = proxyPreferenceManager.getHttpHost(); proxyUser = proxyPreferenceManager.getHttpUser(); proxyPassword = proxyPreferenceManager.getHttpPassword(); } if (DSSUtils.isNotEmpty(proxyUser) && DSSUtils.isNotEmpty(proxyPassword)) { LOG.debug("proxy user: "******":" + proxyPassword); AuthScope proxyAuth = new AuthScope(proxyHost, proxyPort); UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUser, proxyPassword); credsProvider.setCredentials(proxyAuth, proxyCredentials); } LOG.debug("proxy host/port: " + proxyHost + ":" + proxyPort); // TODO SSL peer shut down incorrectly when protocol is https HttpHost proxy = new HttpHost(proxyHost, proxyPort, Protocol.HTTP.getName()); return httpClientBuilder.setProxy(proxy); } catch (MalformedURLException e) { throw new DSSException(e); } }
protected HttpClient getClient() { Credentials defaultcreds = new UsernamePasswordCredentials(this.token, "x"); CredentialsProvider cp = new BasicCredentialsProvider(); cp.setCredentials(new AuthScope(getHost(), -1, AuthScope.ANY_REALM), defaultcreds); HttpClientBuilder clientBuilder = HttpClients.custom() .setDefaultCredentialsProvider(cp) .setRedirectStrategy(new LaxRedirectStrategy()); ProxyConfiguration proxy = Hudson.getInstance().proxy; if (proxy != null) { clientBuilder.setProxy(new HttpHost(proxy.name, proxy.port)); } return clientBuilder.build(); }
/** * Builds the http client. * * @throws KeyManagementException the key management exception * @throws NoSuchAlgorithmException the no such algorithm exception */ private void buildHttpClient() throws KeyManagementException, NoSuchAlgorithmException { if (this.sslsf == null) { this.sslsf = getDefaultSSLSocketFactory(); } HttpClientBuilder httpClientBuilder = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .setSSLSocketFactory(sslsf); if (proxy != null) { httpClientBuilder.setProxy(proxy); } httpClient = httpClientBuilder.build(); }
/** * 使用代理IP抓取页面 Httpclient 4.x * * @throws Exception */ @Test public void test() throws Exception { HttpClientBuilder builder = HttpClients.custom(); // 下面的代理IP和端口就需要从代理Ip库中读取,代理IP也可使用爬虫实时爬取:http://www.xicidaili.com/ String ip = "119.188.94.145"; int port = 80; HttpHost proxy = new HttpHost(ip, port); CloseableHttpClient client = builder.setProxy(proxy).build(); String url = "http://item.jd.com/1514794.html"; HttpGet request = new HttpGet(url); try { CloseableHttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); System.out.println(EntityUtils.toString(entity)); } catch (HttpHostConnectException e) { System.out.println("代理异常,代理IP为:" + ip + ",代理端口为:" + port); } }
/** * Returns the HttpClient through which the REST call is made. Uses an unsafe TrustStrategy in * case the user specified a HTTPS URL and set the ignoreUnverifiedSSLPeer flag. * * @param logger the logger to log messages to * @return the HttpClient */ private HttpClient getHttpClient(PrintStream logger) throws Exception { boolean ignoreUnverifiedSSL = ignoreUnverifiedSSLPeer; String stashServer = stashServerBaseUrl; DescriptorImpl descriptor = getDescriptor(); if ("".equals(stashServer) || stashServer == null) { stashServer = descriptor.getStashRootUrl(); } if (!ignoreUnverifiedSSL) { ignoreUnverifiedSSL = descriptor.isIgnoreUnverifiedSsl(); } URL url = new URL(stashServer); HttpClientBuilder builder = HttpClientBuilder.create(); if (url.getProtocol().equals("https") && ignoreUnverifiedSSL) { // add unsafe trust manager to avoid thrown // SSLPeerUnverifiedException try { TrustStrategy easyStrategy = new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }; SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, easyStrategy).useTLS().build(); SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory( sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); builder.setSSLSocketFactory(sslConnSocketFactory); Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("https", sslConnSocketFactory) .build(); HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry); builder.setConnectionManager(ccm); } catch (NoSuchAlgorithmException nsae) { logger.println("Couldn't establish SSL context:"); nsae.printStackTrace(logger); } catch (KeyManagementException kme) { logger.println("Couldn't initialize SSL context:"); kme.printStackTrace(logger); } catch (KeyStoreException kse) { logger.println("Couldn't initialize SSL context:"); kse.printStackTrace(logger); } } // Configure the proxy, if needed // Using the Jenkins methods handles the noProxyHost settings ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy; if (proxyConfig != null) { Proxy proxy = proxyConfig.createProxy(url.getHost()); if (proxy != null && proxy.type() == Proxy.Type.HTTP) { SocketAddress addr = proxy.address(); if (addr != null && addr instanceof InetSocketAddress) { InetSocketAddress proxyAddr = (InetSocketAddress) addr; HttpHost proxyHost = new HttpHost(proxyAddr.getAddress().getHostAddress(), proxyAddr.getPort()); builder = builder.setProxy(proxyHost); String proxyUser = proxyConfig.getUserName(); if (proxyUser != null) { String proxyPass = proxyConfig.getPassword(); CredentialsProvider cred = new BasicCredentialsProvider(); cred.setCredentials( new AuthScope(proxyHost), new UsernamePasswordCredentials(proxyUser, proxyPass)); builder = builder .setDefaultCredentialsProvider(cred) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); } } } } return builder.build(); }