List of usage examples for org.apache.http.impl.client CloseableHttpClient close
public void close() throws IOException;
From source file:org.blocks4j.reconf.infra.http.layer.SimpleHttpClient.java
private static SimpleHttpResponse execute(CloseableHttpClient httpClient, SimpleHttpRequest request, long timeout, TimeUnit timeunit) throws Exception { RequestTask task = new RequestTask(httpClient, request); Future<SimpleHttpResponse> futureResponse = null; try {/* w ww . j ava2 s . c om*/ futureResponse = requestExecutor.submit(task); return futureResponse.get(timeout, timeunit); } catch (TimeoutException e) { httpClient.close(); RequestLine line = request.getRequestLine(); String method = request.getMethod(); if (line != null && method != null) { throw new TimeoutException(msg.format("error.complete", method.toUpperCase(), line.getUri(), timeout, timeunit.toString().toLowerCase())); } else { throw new TimeoutException(msg.format("error", timeout, timeunit.toString().toLowerCase())); } } catch (Exception e) { httpClient.close(); throw e; } finally { if (futureResponse != null) { futureResponse.cancel(true); } } }
From source file:org.jitsi.meet.test.util.JvbUtil.java
/** * Triggers either force or graceful bridge shutdown and waits for it to * complete.//w w w .jav a 2 s . c om * * @param jvbEndpoint the REST API endpoint of the bridge to be turned off. * @param force <tt>true</tt> if force shutdown should be performed or * <tt>false</tt> to shutdown the bridge gracefully. * * @throws IOException if something goes wrong * @throws InterruptedException if the waiting thread gets interrupted at * any point. */ static public void shutdownBridge(String jvbEndpoint, boolean force) throws IOException, InterruptedException { CloseableHttpClient client = HttpClientBuilder.create().build(); try { triggerShutdown(client, jvbEndpoint, force); try { waitForBridgeShutdown(client, jvbEndpoint); } catch (HttpHostConnectException connectException) { // We ignore connect exception as JVB endpoint // dies on shutdown and may not always send the OK response } } finally { client.close(); } }
From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java
public static String sendGet(String serverURL, String qParams) throws Exception { String url = serverURL + "?" + qParams; CloseableHttpClient client = HttpClients.createDefault(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); // System.out.println("\nSending 'GET' request to URL : " + url); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line);/* w w w.j av a 2 s. c o m*/ } client.close(); return result.toString(); }
From source file:th.ac.kmutt.chart.rest.application.Main.java
private static void systemConfig(SystemM param) { SystemM sys = param;//from w ww . j a va2 s.com String url = "system"; HttpPost httppost = new HttpPost("http://localhost:8081/ChartServices/rest/" + url); XStream xstream = new XStream(new Dom4JDriver()); Class c = null; try { c = Class.forName(sys.getClass().getName()); } catch (ClassNotFoundException e2) { e2.printStackTrace(); } xstream.processAnnotations(c); ByteArrayEntity entity = null; String xString = xstream.toXML(sys); try { entity = new ByteArrayEntity(xString.getBytes("UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } httppost.setEntity(entity); CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpResponse response = null; HttpEntity resEntity = null; try { response = httpclient.execute(httppost); resEntity = response.getEntity(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //httpclient.getConnectionManager().shutdown(); try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:cn.mrdear.pay.util.WebUtils.java
/** * ?//from w ww.ja va 2 s. co m * @param certPath ? * @param passwd ?? * @param uri ? * @param entity xml * @return */ public static String post(String certPath, String passwd, String uri, InputStreamEntity entity) throws Exception { String result = null; KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(certPath)); try { keyStore.load(instream, passwd.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, passwd.toCharArray()).build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { HttpPost httpPost = new HttpPost(uri); entity.setContentEncoding("UTF-8"); httpPost.setEntity(entity); CloseableHttpResponse httpResponse = httpclient.execute(httpPost); result = consumeResponse(httpResponse); } finally { httpclient.close(); } return result; }
From source file:org.keycloak.helper.TestsHelper.java
public static boolean returnsForbidden(String endpoint) throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); try {//from w w w . j a va 2s .c om HttpGet get = new HttpGet(baseUrl + endpoint); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 403 || response.getStatusLine().getStatusCode() == 401) { return true; } else { return false; } } finally { client.close(); } }
From source file:com.jeecms.common.web.ClientCustomSSL.java
public static String getInSsl(String url, File pkcFile, String storeId, String params, String contentType) throws Exception { String text = ""; // ???PKCS12/* w w w .j a v a 2 s. co m*/ KeyStore keyStore = KeyStore.getInstance("PKCS12"); // ?PKCS12? FileInputStream instream = new FileInputStream(pkcFile); try { // PKCS12?(ID) keyStore.load(instream, storeId.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, storeId.toCharArray()).build(); // Allow TLSv1 protocol only // TLS SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); // httpclientSSLSocketFactory CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); try { HttpPost post = new HttpPost(url); StringEntity s = new StringEntity(params, "utf-8"); if (StringUtils.isBlank(contentType)) { s.setContentType("application/xml"); } s.setContentType(contentType); post.setEntity(s); HttpResponse res = httpclient.execute(post); HttpEntity entity = res.getEntity(); text = EntityUtils.toString(entity, "utf-8"); } finally { httpclient.close(); } return text; }
From source file:org.smartloli.kafka.eagle.common.util.HttpClientUtils.java
/** * Send request by get method.//from w w w .j a v a 2s .c o m * * @param uri: * http://ip:port/demo?httpcode=200&name=smartloli */ public static String doGet(String uri) { String result = ""; try { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { HttpGet httpGet = new HttpGet(uri); client = HttpClients.createDefault(); response = client.execute(httpGet); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); } finally { if (response != null) { response.close(); } if (client != null) { client.close(); } } } catch (Exception e) { e.printStackTrace(); LOG.error("Do get request has error, msg is " + e.getMessage()); } return result; }
From source file:org.openo.nfvo.monitor.umc.monitor.common.HttpClientUtil.java
/** * @Title httpPost/*from w w w. j av a 2 s . c o m*/ * @Description TODO(realize the rest interface to access by httpPost) * @param url * @throws Exception * @return int (return StatusCode,If zero error said) */ public static int httpPost(String url) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); try { CloseableHttpResponse res = httpClient.execute(httpPost); try { return res.getStatusLine().getStatusCode(); } finally { res.close(); } } catch (ParseException e) { LOGGER.error("HttpClient throw ParseException:" + url, e); } catch (IOException e) { LOGGER.error("HttpClient throw IOException:" + url, e); } finally { try { httpClient.close(); } catch (IOException e) { // TODO Auto-generated catch block LOGGER.error("HttpClient Close throw IOException", e); } } return 0; }
From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java
public static void sendPut(String serverURL, String postBody) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); HttpPut post = new HttpPut(serverURL); StringEntity entity = new StringEntity(postBody, "UTF-8"); post.setEntity(entity);// w w w .j a va 2 s . c o m HttpResponse response = client.execute(post); // System.out.println("\nSending 'PUT' request to URL : " + serverURL); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } client.close(); // System.out.println(result.toString()); }