Java Code Examples for org.apache.http.impl.client.HttpClientBuilder#create()
The following examples show how to use
org.apache.http.impl.client.HttpClientBuilder#create() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpTransactionExecutor.java From SmartApplianceEnabler with GNU General Public License v2.0 | 6 votes |
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; } }
Example 2
Source File: TestHttpClient4.java From davmail with GNU General Public License v2.0 | 6 votes |
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); } } }
Example 3
Source File: HttpClientManager.java From hop with Apache License 2.0 | 6 votes |
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(); }
Example 4
Source File: HttpSettings.java From logsniffer with GNU Lesser General Public License v3.0 | 6 votes |
/** * 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; }
Example 5
Source File: ModelUtils.java From gluon-samples with BSD 3-Clause "New" or "Revised" License | 5 votes |
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()); } }
Example 6
Source File: HttpInvokerConnector.java From rice with Educational Community License v2.0 | 5 votes |
/** * 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(); }
Example 7
Source File: TestHttpClient4.java From davmail with GNU General Public License v2.0 | 5 votes |
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); } } }
Example 8
Source File: CmmnHttpActivityBehaviorImpl.java From flowable-engine with Apache License 2.0 | 5 votes |
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()); }
Example 9
Source File: UrkundAPIUtil.java From sakai with Educational Community License v2.0 | 5 votes |
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; }
Example 10
Source File: FormControllerTest.java From mangooio with Apache License 2.0 | 5 votes |
@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")); }
Example 11
Source File: PostReceiver.java From android-lite-http with Apache License 2.0 | 5 votes |
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(); } }
Example 12
Source File: HttpClientInstance.java From sofa-tracer with Apache License 2.0 | 5 votes |
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; }
Example 13
Source File: AbstractBimServerClientFactory.java From BIMserver with GNU Affero General Public License v3.0 | 5 votes |
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(); }
Example 14
Source File: HttpHelper.java From canal with Apache License 2.0 | 5 votes |
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(); } }
Example 15
Source File: LUISGrammarEvaluator.java From JVoiceXML with GNU Lesser General Public License v2.1 | 5 votes |
/** * {@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; } }
Example 16
Source File: HttpClientUtils.java From rpc-benchmark with Apache License 2.0 | 5 votes |
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(); }
Example 17
Source File: HttpClientUtils.java From ais-sdk with Apache License 2.0 | 4 votes |
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; }
Example 18
Source File: WebUtil.java From dal with Apache License 2.0 | 4 votes |
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; }
Example 19
Source File: GatewayBasicFuncTest.java From knox with Apache License 2.0 | 4 votes |
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" ); }
Example 20
Source File: JenkinsHttpClient.java From jenkins-client-java with MIT License | 2 votes |
/** * 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); }