use of org.apache.http.impl.client.HttpClientBuilder in project intellij-community by JetBrains.
the class GithubConnectionBuilder method createClient.
@NotNull
public CloseableHttpClient createClient() {
HttpClientBuilder builder = HttpClients.custom();
builder.setDefaultRequestConfig(createRequestConfig()).setDefaultConnectionConfig(createConnectionConfig()).setDefaultHeaders(createHeaders()).setSslcontext(CertificateManager.getInstance().getSslContext());
setupCredentialsProvider(builder);
return builder.build();
}
use of org.apache.http.impl.client.HttpClientBuilder in project openstack4j by ContainX.
the class HttpClientFactory method buildClient.
private CloseableHttpClient buildClient(Config config) {
HttpClientBuilder cb = HttpClientBuilder.create().setUserAgent(USER_AGENT);
if (config.getProxy() != null) {
try {
URL url = new URL(config.getProxy().getHost());
HttpHost proxy = new HttpHost(url.getHost(), config.getProxy().getPort(), url.getProtocol());
cb.setProxy(proxy);
} catch (MalformedURLException e) {
LOG.error(e.getMessage(), e);
}
}
if (config.isIgnoreSSLVerification()) {
cb.setSslcontext(UntrustedSSL.getSSLContext());
cb.setHostnameVerifier(new AllowAllHostnameVerifier());
}
if (config.getSslContext() != null)
cb.setSslcontext(config.getSslContext());
if (config.getMaxConnections() > 0) {
cb.setMaxConnTotal(config.getMaxConnections());
}
if (config.getMaxConnectionsPerRoute() > 0) {
cb.setMaxConnPerRoute(config.getMaxConnectionsPerRoute());
}
RequestConfig.Builder rcb = RequestConfig.custom();
if (config.getConnectTimeout() > 0)
rcb.setConnectTimeout(config.getConnectTimeout());
if (config.getReadTimeout() > 0)
rcb.setSocketTimeout(config.getReadTimeout());
if (INTERCEPTOR != null) {
INTERCEPTOR.onClientCreate(cb, rcb, config);
}
return cb.setDefaultRequestConfig(rcb.build()).build();
}
use of org.apache.http.impl.client.HttpClientBuilder in project pact-jvm by DiUS.
the class InsecureHttpsRequest method setupInsecureSSL.
private void setupInsecureSSL() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
TrustStrategy trustStrategy = (chain, authType) -> true;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
b.setConnectionManager(connMgr);
// finally, build the HttpClient;
// -- done!
this.httpclient = b.build();
}
use of org.apache.http.impl.client.HttpClientBuilder in project SmartApplianceEnabler by camueller.
the class HttpTransactionExecutor method sendHttpRequest.
/**
* Send a HTTP request whose response has to be closed by the caller!
* @param url
* @return
*/
protected CloseableHttpResponse sendHttpRequest(String url, String data, ContentType contentType, String username, String password) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (username != null && password != null) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClientBuilder.setDefaultCredentialsProvider(provider);
}
CloseableHttpClient client = httpClientBuilder.build();
logger.debug("Sending HTTP request");
logger.debug("url=" + url);
logger.debug("data=" + data);
logger.debug("contentType=" + contentType);
logger.debug("username=" + username);
logger.debug("password=" + password);
try {
HttpRequestBase request = null;
if (data != null) {
request = new HttpPost(url);
((HttpPost) request).setEntity(new StringEntity(data, contentType));
} else {
request = new HttpGet(url);
}
CloseableHttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
logger.debug("Response code is " + responseCode);
return response;
} catch (IOException e) {
logger.error("Error executing HTTP request.", e);
return null;
}
}
use of org.apache.http.impl.client.HttpClientBuilder in project weixin-java-tools by chanjarster.
the class WxMpServiceImpl method setWxMpConfigStorage.
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
this.wxMpConfigStorage = wxConfigProvider;
String http_proxy_host = wxMpConfigStorage.getHttp_proxy_host();
int http_proxy_port = wxMpConfigStorage.getHttp_proxy_port();
String http_proxy_username = wxMpConfigStorage.getHttp_proxy_username();
String http_proxy_password = wxMpConfigStorage.getHttp_proxy_password();
final HttpClientBuilder builder = HttpClients.custom();
if (StringUtils.isNotBlank(http_proxy_host)) {
// 使用代理服务器
if (StringUtils.isNotBlank(http_proxy_username)) {
// 需要用户认证的代理服务器
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(http_proxy_host, http_proxy_port), new UsernamePasswordCredentials(http_proxy_username, http_proxy_password));
builder.setDefaultCredentialsProvider(credsProvider);
} else {
// 无需用户认证的代理服务器
}
httpProxy = new HttpHost(http_proxy_host, http_proxy_port);
}
if (wxConfigProvider.getSSLContext() != null) {
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(wxConfigProvider.getSSLContext(), new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
builder.setSSLSocketFactory(sslsf);
}
httpClient = builder.build();
}
Aggregations