下面列出了org.apache.http.impl.client.HttpClientBuilder#create() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public CloseableHttpResponse post(String url, ContentType contentType, String data, String username, String password) {
logger.debug("{}: Sending POST request url={} contentType={} data={}", applianceId, url, contentType, data);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
withUsernameAndPassword(httpClientBuilder, username, password);
CloseableHttpClient client = httpClientBuilder.setDefaultRequestConfig(getRequestConfig()).build();
try {
HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity(data, contentType));
CloseableHttpResponse response = client.execute(request);
return logResponse(response);
}
catch(IOException e) {
logger.error("{}: Error executing POST", applianceId, e);
return null;
}
}
public CloseableHttpClient build() {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager( manager );
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
if ( socketTimeout > 0 ) {
requestConfigBuilder.setSocketTimeout( socketTimeout );
}
if ( connectionTimeout > 0 ) {
requestConfigBuilder.setConnectTimeout( socketTimeout );
}
if ( proxy != null ) {
requestConfigBuilder.setProxy( proxy );
}
httpClientBuilder.setDefaultRequestConfig( requestConfigBuilder.build() );
if ( provider != null ) {
httpClientBuilder.setDefaultCredentialsProvider( provider );
}
if ( redirectStrategy != null ) {
httpClientBuilder.setRedirectStrategy( redirectStrategy );
}
return httpClientBuilder.build();
}
/**
* Creates a {@link HttpClientBuilder} considering current settings.
*
* @return a {@link HttpClientBuilder} considering current settings.
*/
public HttpClientBuilder createHttpClientBuilder() {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (getHttpProxy() != null
&& StringUtils.isNotBlank(getHttpProxy().getHost())) {
httpClientBuilder = httpClientBuilder.setProxy(new HttpHost(
getHttpProxy().getHost(), getHttpProxy().getPort(),
getHttpProxy().getSchema()));
if (StringUtils.isNotBlank(getHttpProxy().getUser())) {
Credentials credentials = new UsernamePasswordCredentials(
getHttpProxy().getUser(), getHttpProxy().getPassword());
AuthScope authScope = new AuthScope(getHttpProxy().getHost(),
getHttpProxy().getPort());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope, credentials);
httpClientBuilder = httpClientBuilder
.setDefaultCredentialsProvider(credsProvider);
httpClientBuilder.useSystemProperties();
}
}
return httpClientBuilder;
}
public void testHttpProxy() throws IOException {
Settings.setLoggingLevel("org.apache.http.wire", Level.DEBUG);
Settings.setLoggingLevel("org.apache.http", Level.DEBUG);
String proxyHost = Settings.getProperty("davmail.proxyHost");
int proxyPort = Settings.getIntProperty("davmail.proxyPort");
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setProxy(proxy).setUserAgent(DavGatewayHttpClientFacade.IE_USER_AGENT);
clientBuilder.setDefaultCredentialsProvider(getProxyCredentialProvider());
try (CloseableHttpClient httpClient = clientBuilder.build()) {
HttpGet httpget = new HttpGet("http://davmail.sourceforge.net/version.txt");
try (CloseableHttpResponse response = httpClient.execute(httpget)) {
String responseString = new BasicResponseHandler().handleResponse(response);
System.out.println(responseString);
}
}
}
public static void getMnistPNG() throws IOException {
String tmpDirStr = System.getProperty("java.io.tmpdir");
String archizePath = DATA_PATH + "/mnist_png.tar.gz";
if (tmpDirStr == null) {
throw new IOException("System property 'java.io.tmpdir' does specify a tmp dir");
}
String url = "http://github.com/myleott/mnist_png/raw/master/mnist_png.tar.gz";
File f = new File(archizePath);
File dir = new File(tmpDirStr);
if (!f.exists()) {
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
try (CloseableHttpResponse response = client.execute(new HttpGet(url))) {
HttpEntity entity = response.getEntity();
if (entity != null) {
try (FileOutputStream outstream = new FileOutputStream(f)) {
entity.writeTo(outstream);
outstream.flush();
outstream.close();
}
}
}
System.out.println("Data downloaded to " + f.getAbsolutePath());
} else {
System.out.println("Using existing directory at " + f.getAbsolutePath());
}
}
public static CloseableHttpClient createHttpClient(int concurrency) {
HttpClientBuilder builder = HttpClientBuilder.create();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(concurrency);
connManager.setMaxTotal(concurrency);
RequestConfig requestConfig = RequestConfig.custom()//
.setAuthenticationEnabled(true)//
.setSocketTimeout(SOCKET_TIMEOUT)//
.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)//
.setConnectTimeout(CONNECT_TIMEOUT)//
.setRedirectsEnabled(true)//
.setRelativeRedirectsAllowed(true)//
.setMaxRedirects(15)//
.build();
SocketConfig socketConfig = SocketConfig.custom()//
.setSoKeepAlive(true)//
.setSoReuseAddress(true)//
.build();
builder.setConnectionManager(connManager);
builder.setDefaultSocketConfig(socketConfig);
builder.setDefaultRequestConfig(requestConfig);
return builder.build();
}
/**
* {@inheritDoc}
*/
@Override
public Object getSemanticInterpretation(final DataModel model,
String utterance) {
final HttpClientBuilder builder = HttpClientBuilder.create();
if (PROXY_HOST != null) {
HttpHost proxy = new HttpHost(PROXY_HOST, PROXY_PORT);
builder.setProxy(proxy);
}
try (CloseableHttpClient client = builder.build()){
final URIBuilder uribuilder = new URIBuilder(grammarUri);
uribuilder.addParameter("subscription-key", subscriptionKey);
uribuilder.addParameter("q", utterance);
final URI uri = uribuilder.build();
final HttpGet request = new HttpGet(uri);
final HttpResponse response = client.execute(request);
final StatusLine statusLine = response.getStatusLine();
final int status = statusLine.getStatusCode();
if (status != HttpStatus.SC_OK) {
final String reasonPhrase = statusLine.getReasonPhrase();
LOGGER.error("error accessing '" + uri +"': " +
reasonPhrase + " (HTTP error code "
+ status + ")");
return null;
}
final HttpEntity entity = response.getEntity();
final InputStream input = entity.getContent();
final Object interpretation = parseLUISResponse(model, input);
return interpretation;
} catch (IOException | URISyntaxException | ParseException | SemanticError e) {
LOGGER.error(e.getMessage(), e);
return null;
}
}
public static byte[] getBytes(String url, int timeout) throws Exception {
long start = System.currentTimeMillis();
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setMaxConnPerRoute(50);
builder.setMaxConnTotal(100);
CloseableHttpClient httpclient = builder.build();
URI uri = new URIBuilder(url).build();
RequestConfig config = custom().setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build();
HttpGet httpGet = new HttpGet(uri);
HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(config);
CloseableHttpResponse response = httpclient.execute(httpGet, context);
try {
int statusCode = response.getStatusLine().getStatusCode();
long end = System.currentTimeMillis();
long cost = end - start;
if (logger.isWarnEnabled()) {
logger.warn("post " + url + ", cost : " + cost);
}
if (statusCode == HttpStatus.SC_OK) {
return EntityUtils.toByteArray(response.getEntity());
} else {
String errorMsg = EntityUtils.toString(response.getEntity());
throw new RuntimeException("requestGet remote error, url=" + uri.toString() + ", code=" + statusCode
+ ", error msg=" + errorMsg);
}
} finally {
response.close();
httpGet.releaseConnection();
}
}
public void initHttpClient() {
HttpClientBuilder builder = HttpClientBuilder.create();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(100);
builder.setConnectionManager(connManager);
builder.disableAutomaticRetries();
// builder.addInterceptorFirst(new HttpRequestInterceptor() {
// public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
// if (!request.containsHeader("Accept-Encoding")) {
// request.addHeader("Accept-Encoding", "gzip");
// }
// }
// });
//
// builder.addInterceptorFirst(new HttpResponseInterceptor() {
// public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
// HttpEntity entity = response.getEntity();
// if (entity != null) {
// Header ceheader = entity.getContentEncoding();
// if (ceheader != null) {
// HeaderElement[] codecs = ceheader.getElements();
// for (int i = 0; i < codecs.length; i++) {
// if (codecs[i].getName().equalsIgnoreCase("gzip")) {
// response.setEntity(new GzipDecompressingEntity(response.getEntity()));
// return;
// }
// }
// }
// }
// }
// });
httpClient = builder.build();
}
private CloseableHttpClient getHttpClient() {
if (this.httpClient != null) {
return this.httpClient;
}
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(6);
connManager.setMaxTotal(20);
connManager.closeIdleConnections(120, TimeUnit.SECONDS);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//SOFATracer
SofaTracerHttpClientBuilder.clientBuilder(httpClientBuilder);
httpClient = httpClientBuilder.setConnectionManager(connManager).disableAutomaticRetries()
.setUserAgent("CLIENT_VERSION").build();
return httpClient;
}
private void sendRequest(HttpEntity entity) {
try {
HttpPost post = new HttpPost("http://localhost:8080/get?a=1&b=b.34&a=4");
post.setEntity(entity);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient hc = httpClientBuilder.build();
hc.execute(post);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testSingleFileUpload(@TempDir Path tempDir) throws IOException {
// given
String host = Application.getInstance(Config.class).getConnectorHttpHost();
int port = Application.getInstance(Config.class).getConnectorHttpPort();
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// when
Path path = tempDir.resolve(UUID.randomUUID().toString());
InputStream attachment = Resources.getResource("attachment.txt").openStream();
Files.copy(attachment, path);
multipartEntityBuilder.addPart("attachment", new FileBody(path.toFile()));
HttpPost httpPost = new HttpPost("http://" + host + ":" + port + "/singlefile");
httpPost.setEntity(multipartEntityBuilder.build());
String response = null;
HttpResponse httpResponse = httpClientBuilder.build().execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
response = EntityUtils.toString(httpEntity);
}
// then
assertThat(httpResponse, not(nullValue()));
assertThat(response, not(nullValue()));
assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(StatusCodes.OK));
assertThat(response, equalTo("This is an attachment"));
}
public static String getFileInfo(String baseUrl, String externalId, String receiverAddress, String urkundUsername, String urkundPassword, int timeout){
String ret = null;
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setConnectTimeout(timeout);
requestBuilder = requestBuilder.setConnectionRequestTimeout(timeout);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setDefaultRequestConfig(requestBuilder.build());
try (CloseableHttpClient httpClient = builder.build()) {
HttpGet httpget = new HttpGet(baseUrl+"submissions/"+receiverAddress+"/"+externalId);
//------------------------------------------------------------
if(StringUtils.isNotBlank(urkundUsername) && StringUtils.isNotBlank(urkundPassword)) {
addAuthorization(httpget, urkundUsername, urkundPassword);
}
//------------------------------------------------------------
httpget.addHeader("Accept", "application/json");
//------------------------------------------------------------
HttpResponse response = httpClient.execute(httpget);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
ret = EntityUtils.toString(resEntity);
EntityUtils.consume(resEntity);
}
} catch (IOException e) {
log.error("ERROR getting File Info : ", e);
}
return ret;
}
protected HttpActivityExecutor createHttpActivityExecutor() {
HttpClientConfig config = CommandContextUtil.getCmmnEngineConfiguration().getHttpClientConfig();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// https settings
if (config.isDisableCertVerify()) {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
httpClientBuilder.setSSLSocketFactory(
new SSLConnectionSocketFactory(builder.build(), new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}));
} catch (Exception e) {
LOGGER.error("Could not configure HTTP client SSL self signed strategy", e);
}
}
// request retry settings
int retryCount = 0;
if (config.getRequestRetryLimit() > 0) {
retryCount = config.getRequestRetryLimit();
}
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, false));
// client builder settings
if (config.isUseSystemProperties()) {
httpClientBuilder.useSystemProperties();
}
return new HttpActivityExecutor(httpClientBuilder, new NopErrorPropagator(),
CommandContextUtil.getCmmnEngineConfiguration().getObjectMapper());
}
public void testBasicGetRequest() throws IOException {
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
try (CloseableHttpClient httpClient = clientBuilder.build()) {
HttpGet httpget = new HttpGet("http://davmail.sourceforge.net/version.txt");
try (CloseableHttpResponse response = httpClient.execute(httpget)) {
String responseString = new BasicResponseHandler().handleResponse(response);
System.out.println(responseString);
}
}
}
/**
* Creates a httpcomponents HttpClient for service invocation.
*
* <p>The client is configured by the HttpClientConfigurer</p>
*/
public HttpClient getHttpClient() {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
getHttpClientConfigurer().customizeHttpClient(httpClientBuilder);
return httpClientBuilder.build();
}
private String oozieSubmitJob( String user, String password, String request, int status ) throws IOException, URISyntaxException {
driver.getMock( "OOZIE" )
.expect()
.method( "POST" )
.pathInfo( "/v1/jobs" )
.respond()
.status( HttpStatus.SC_CREATED )
.content( driver.getResourceBytes( "oozie-jobs-submit-response.json" ) )
.contentType( "application/json" );
URL url = new URL( driver.getUrl( "OOZIE" ) + "/v1/jobs?action=start" + ( driver.isUseGateway() ? "" : "&user.name=" + user ) );
HttpHost targetHost = new HttpHost( url.getHost(), url.getPort(), url.getProtocol() );
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient client = builder.build();
HttpClientContext context = HttpClientContext.create();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope( targetHost ),
new UsernamePasswordCredentials( user, password ) );
context.setCredentialsProvider( credsProvider );
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put( targetHost, basicAuth );
// Add AuthCache to the execution context
context.setAuthCache( authCache );
HttpPost post = new HttpPost( url.toURI() );
// post.getParams().setParameter( "action", "start" );
StringEntity entity = new StringEntity( request, org.apache.http.entity.ContentType.create( "application/xml", StandardCharsets.UTF_8.name() ) );
post.setEntity( entity );
post.setHeader( "X-XSRF-Header", "ksdjfhdsjkfhds" );
HttpResponse response = client.execute( targetHost, post, context );
assertThat( response.getStatusLine().getStatusCode(), Matchers.is(status) );
String json = EntityUtils.toString( response.getEntity() );
// String json = given()
// .log().all()
// .auth().preemptive().basic( user, password )
// .queryParam( "action", "start" )
// .contentType( "application/xml;charset=UTF-8" )
// .content( request )
// .then()
// .log().all()
// .statusCode( status )
// .when().post( getUrl( "OOZIE" ) + "/v1/jobs" + ( isUseGateway() ? "" : "?user.name=" + user ) ).asString();
return JsonPath.from(json).getString( "id" );
}
private static HttpClient initWeakSSLClient() {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = null;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) {
return true;
}
}).build();
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
// do nothing, has been handled outside
}
b.setSslcontext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
X509HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
// 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);
/**
* Set timeout option
*/
RequestConfig.Builder configBuilder = RequestConfig.custom();
configBuilder.setConnectTimeout(TIMEOUT);
configBuilder.setSocketTimeout(TIMEOUT);
b.setDefaultRequestConfig(configBuilder.build());
// finally, build the HttpClient;
// -- done!
HttpClient sslClient = b.build();
return sslClient;
}
public static CloseableHttpClient acceptsUntrustedCertsHttpClient(boolean withProxy, ProxyHostInfo hostInfo, int connectionTimeout, int connectionRequestTimeout, int socketTimeout)
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
/**
* set http proxy
*/
b.setDefaultRequestConfig(
RequestConfig.custom().setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout).build()
);
if(withProxy){
HttpHost proxy=new HttpHost(hostInfo.getHostName(),hostInfo.getPort());
b.setProxy(proxy);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxy.getHostName(), proxy.getPort()),
new UsernamePasswordCredentials(hostInfo.getUserName(), hostInfo.getPassword()));
b.setDefaultCredentialsProvider(credsProvider);
}
SSLContext sslContext = new SSLContextBuilder().useProtocol("TLSv1.2").loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
b.setSSLContext(sslContext);
b.setConnectionTimeToLive(180, TimeUnit.SECONDS);
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory)
.build();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connMgr.setMaxTotal(200);
connMgr.setDefaultMaxPerRoute(100);
b.setConnectionManager(connMgr);
CloseableHttpClient client = b.build();
return client;
}
/**
* Create an authenticated Jenkins HTTP client
*
* @param uri Location of the jenkins server (ex. http://localhost:8080)
* @param username Username to use when connecting
* @param password Password or auth token to use when connecting
*/
public JenkinsHttpClient(URI uri, String username, String password) {
this(uri, HttpClientBuilder.create(), username, password);
}