List of usage examples for org.apache.http.impl.client CloseableHttpClient close
public void close() throws IOException;
From source file:com.jiuyi.qujiuyi.common.util.WxRefundSSL.java
public final static String post(String entity, String mch_id, Integer clientType) throws Exception { try {// ww w .ja v a2 s . c o m KeyStore keyStore = KeyStore.getInstance("PKCS12"); // FileInputStream instream = new FileInputStream(new // File("D:\\apiclient_cert.p12")); FileInputStream instream = null; if (clientType == 0) { instream = new FileInputStream(new File(SysCfg.getString("apiclient.ssl"))); } else { instream = new FileInputStream(new File(SysCfg.getString("apiclient.app.ssl"))); } try { keyStore.load(instream, mch_id.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mch_id.toCharArray()).build(); sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); } catch (Exception e) { e.printStackTrace(); } CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); String result = ""; try { HttpPost post = new HttpPost(SysCfg.getString("weixin.refund")); post.setEntity(new StringEntity(entity)); CloseableHttpResponse response = httpclient.execute(post); try { HttpEntity resp = response.getEntity(); if (resp != null) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resp.getContent())); String line = null; while ((line = bufferedReader.readLine()) != null) { result += line; } } EntityUtils.consume(resp); } finally { response.close(); } } finally { httpclient.close(); } return result; }
From source file:DeliverWork.java
private static String getResultBody(String url) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; String resultBody = null;/* ww w . j av a 2 s . c o m*/ try { httpClient = HttpClients.createDefault(); // http(get?) HttpGet httpget = new HttpGet(url); response = httpClient.execute(httpget); HttpEntity result = response.getEntity(); resultBody = EntityUtils.toString(result); } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return resultBody; }
From source file:com.beginner.core.utils.HttpUtil.java
/** * <p>To request the GET way.</p> * // w w w .j a va 2s .c om * @param url request URI * @param timeout request timeout time in milliseconds(The default timeout time for 30 seconds.) * @return String response result * @throws Exception * @since 1.0.0 */ public static String get(String url, Integer timeout) throws Exception { // Validates input if (StringUtils.isBlank(url)) throw new IllegalArgumentException("The url cannot be null and cannot be empty."); //The default timeout time for 30 seconds if (null == timeout) timeout = 30000; CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse httpResponse = null; String result = null; try { RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout) .setConnectTimeout(timeout).build(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); result = EntityUtils.toString(entity); EntityUtils.consume(entity); } catch (Exception e) { logger.error("GET?", e); return null; } finally { if (null != httpResponse) httpResponse.close(); if (null != httpClient) httpClient.close(); } return result; }
From source file:org.drftpd.util.HttpUtils.java
public static String retrieveHttpAsString(String url) throws HttpException, IOException { RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000) .setConnectionRequestTimeout(5000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig) .setUserAgent(_userAgent).build(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig);// w ww.j a v a 2 s.c o m CloseableHttpResponse response = null; try { response = httpclient.execute(httpGet); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new HttpException("Error " + statusCode + " for URL " + url); } HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity); EntityUtils.consume(entity); return data; } catch (IOException e) { throw new IOException("Error for URL " + url, e); } finally { if (response != null) { response.close(); } httpclient.close(); } }
From source file:org.jboss.as.test.integration.domain.AbstractSSLMasterSlaveTestCase.java
private static ModelNode executeOverHttp(URI mgmtURI, String operation) throws IOException { CloseableHttpClient httpClient = createHttpClient(mgmtURI); HttpEntity operationEntity = new StringEntity(operation, ContentType.APPLICATION_JSON); HttpPost httpPost = new HttpPost(mgmtURI); httpPost.setEntity(operationEntity); HttpResponse response;/*from ww w.j a v a 2 s .c om*/ ModelNode responseNode; try { response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { return null; } HttpEntity entity = response.getEntity(); if (entity == null) { return null; } responseNode = ModelNode.fromJSONStream(response.getEntity().getContent()); EntityUtils.consume(entity); } finally { httpClient.close(); } return responseNode; }
From source file:org.everit.osgi.authentication.cas.tests.SampleApp.java
public static void pingCasLoginUrl(final BundleContext bundleContext) throws Exception { CloseableHttpClient httpClient = new SecureHttpClient(null, bundleContext).getHttpClient(); HttpGet httpGet = new HttpGet(CAS_LOGIN_URL + "?" + LOCALE); HttpResponse httpResponse = null;// www . j a v a 2 s . c om try { httpResponse = httpClient.execute(httpGet); Assert.assertEquals(CAS_PING_FAILURE_MESSAGE, HttpServletResponse.SC_OK, httpResponse.getStatusLine().getStatusCode()); } catch (Exception e) { e.printStackTrace(); Assert.fail(CAS_PING_FAILURE_MESSAGE); } finally { if (httpResponse != null) { EntityUtils.consume(httpResponse.getEntity()); } httpClient.close(); } }
From source file:com.zhch.example.commons.http.v4_5.ClientExecuteProxy.java
public static void proxyExample() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/* w w w .ja va2 s . com*/ HttpHost proxy = new HttpHost("24.157.37.61", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("http://www.ip.cn"); request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1"); request.setConfig(config); System.out.println( "Executing request " + request.getRequestLine() + " to " + request.getURI() + " via " + proxy); CloseableHttpResponse response = httpclient.execute(request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println(EntityUtils.toString(response.getEntity(), "utf8")); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:de.undercouch.gradle.tasks.download.internal.CachingHttpClientFactory.java
/** * Close all HTTP clients created by this factory * @throws IOException if an I/O error occurs *///from w w w.j av a 2 s . c o m public void close() throws IOException { for (CloseableHttpClient c : cachedAcceptingClients.values()) { c.close(); } cachedAcceptingClients.clear(); for (CloseableHttpClient c : cachedClients.values()) { c.close(); } cachedClients.clear(); }
From source file:org.openo.nfvo.emsdriver.northbound.client.HttpClientUtil.java
public static String doGet(String url, String charset) { CloseableHttpClient httpClient = null; HttpGet httpGet = null;/*from w w w.j ava 2s . c o m*/ String result = null; try { httpClient = HttpClientFactory.getSSLClientFactory(); httpGet = new HttpGet(url); CloseableHttpResponse response = httpClient.execute(httpGet); try { if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception e) { log.error("", e); } finally { if (response != null) { response.close(); } } } catch (Exception e) { log.error("doGet is fail ", e); } finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { } } } return result; }
From source file:cat.calidos.morfeu.utils.HttpClientModuleTest.java
@Test public void testProduceHttpClient() throws Exception { CloseableHttpClient client = HttpClientModule.produceHttpClient(); assertNotNull(client);//from w w w. ja va 2s . com client.close(); }