List of usage examples for org.apache.http.client.methods CloseableHttpResponse close
public void close() throws IOException;
From source file:com.continuuity.loom.TestHelper.java
public static SchedulableTask takeTask(String loomUrl, TakeTaskRequest request) throws Exception { HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/take", loomUrl)); httpPost.setEntity(new StringEntity(GSON.toJson(request))); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); try {/*www . ja v a 2s. c om*/ Assert.assertEquals(2, response.getStatusLine().getStatusCode() / 100); if (response.getEntity() == null) { return null; } return GSON.fromJson(EntityUtils.toString(response.getEntity()), SchedulableTask.class); } finally { response.close(); } }
From source file:com.simple.weixin.refund.ClientCustomSSL.java
public static String doRefund(String password, String keyStrore, String url, String data) throws Exception { /**// w w w. jav a 2 s. com * ?PKCS12? ?-- API */ KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(keyStrore));//P12 try { /** * ? * */ keyStore.load(instream, password.toCharArray());//?..MCHID } finally { instream.close(); } // Trust own CA and all self-signed certs /** * ? * */ SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password.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 httpost = new HttpPost(url); // ?? httpost.addHeader("Connection", "keep-alive"); httpost.addHeader("Accept", "*/*"); httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httpost.addHeader("Host", "api.mch.weixin.qq.com"); httpost.addHeader("X-Requested-With", "XMLHttpRequest"); httpost.addHeader("Cache-Control", "max-age=0"); httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "); httpost.setEntity(new StringEntity(data, "UTF-8")); CloseableHttpResponse response = httpclient.execute(httpost); try { HttpEntity entity = response.getEntity(); String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8"); EntityUtils.consume(entity); return jsonStr; } finally { response.close(); } } finally { httpclient.close(); } }
From source file:org.sahli.asciidoc.confluence.publisher.client.http.ConfluenceRestClient.java
private static void closeResponse(CloseableHttpResponse response) { try {// ww w. j a va 2 s .c om response.close(); } catch (IOException e) { throw new RuntimeException("Could not close response", e); } }
From source file:org.dbrane.cloud.util.httpclient.HttpClientHelper.java
/** * ?Http/*from w w w. ja v a 2 s .co m*/ * * @param request * @return */ private static String getResult(HttpRequestBase request) { CloseableHttpClient httpClient = getHttpClient(); try { CloseableHttpResponse response = httpClient.execute(request); response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity); response.close(); if (200 == response.getStatusLine().getStatusCode()) { return result; } else { throw new BaseException(ErrorType.Httpclient, result); } } return EMPTY_STR; } catch (Exception e) { logger.debug(ErrorType.Httpclient.toString(), e); throw new BaseException(ErrorType.Httpclient, e); } finally { } }
From source file:cn.digirun.frame.payment.wxpay.util.ClientCustomSSL.java
public static String doRefund(String url, String data) throws Exception { /**/*from ww w . j av a 2 s . co m*/ * ?PKCS12? ?-- API */ KeyStore keyStore = KeyStore.getInstance("PKCS12"); /** * ? */ //ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+ ""); // FileInputStream instream = new FileInputStream(new File("D:/Program Files/MyEclipse 6.5/workspace/weidian/WebRoot/cer/apiclient_cert.p12"));//P12 FileInputStream instream = new FileInputStream( ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + WxpayConfig.cert_path)); try { /** * ? * MCHID * */ keyStore.load(instream, WxpayConfig.mch_id.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, WxpayConfig.mch_id.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 httpost = new HttpPost(url); // ?? httpost.addHeader("Connection", "keep-alive"); httpost.addHeader("Accept", "*/*"); httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httpost.addHeader("Host", "api.mch.weixin.qq.com"); httpost.addHeader("X-Requested-With", "XMLHttpRequest"); httpost.addHeader("Cache-Control", "max-age=0"); httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) "); httpost.setEntity(new StringEntity(data, "UTF-8")); CloseableHttpResponse response = httpclient.execute(httpost); try { HttpEntity entity = response.getEntity(); String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8"); EntityUtils.consume(entity); return jsonStr; } finally { response.close(); } } finally { httpclient.close(); } }
From source file:org.lokra.seaweedfs.util.ConnectionUtil.java
/** * Check uri link is alive, the basis for judging response status code. * * @param client httpClient//from w ww. j a va2s .c om * @param url check url * @return When the response status code is 200, the result is true. */ public static boolean checkUriAlive(CloseableHttpClient client, String url) { boolean result = false; CloseableHttpResponse response = null; HttpGet request = new HttpGet(url); try { response = client.execute(request, HttpClientContext.create()); result = response.getStatusLine().getStatusCode() == HttpStatus.SC_OK; } catch (IOException e) { return false; } finally { if (response != null) { try { response.close(); } catch (IOException ignored) { } } request.releaseConnection(); } return result; }
From source file:org.wso2.carbon.ml.MLTestUtils.java
public static String getJsonArrayAsString(CloseableHttpResponse response) throws IOException, JSONException { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); JSONArray responseJson = new JSONArray(bufferedReader.readLine()); bufferedReader.close(); response.close();//from www . ja v a2 s .c o m return responseJson.toString(); }
From source file:fr.lissi.belilif.om2m.rest.WebServiceActions.java
/** * Do post.//from w w w . ja v a 2 s . com * * @param uri * the uri * @param body * the body * @param headers * the headers * @return the int * @throws ClientProtocolException * the client protocol exception * @throws IOException * Signals that an I/O exception has occurred. * @throws HttpResponseException * the http response exception */ public static int doPost(String uri, String body, HashMap<String, String> headers) throws ClientProtocolException, IOException, HttpResponseException { // TODO delete before commit // System.out.println("doPost>> uri:"+uri +"\nbody:"+body+"\n"); CloseableHttpClient httpclient = HttpClients.createDefault(); int resp = -1; try { HttpPost httpPost = new HttpPost(uri); for (String key : headers.keySet()) { httpPost.addHeader(key, headers.get(key)); // System.out.println("header:"+key+"/"+headers.get(key)); } // System.out.println("doPost<<"); httpPost.setEntity(new StringEntity(body)); CloseableHttpResponse response = httpclient.execute(httpPost); resp = response.getStatusLine().getStatusCode(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) { response.close(); } else { throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); } } finally { httpclient.close(); } return resp; }
From source file:org.keycloak.testsuite.saml.ConcurrentAuthnRequestTest.java
public static void performLogin(HttpUriRequest post, URI samlEndpoint, String relayState, Document samlRequest, CloseableHttpResponse response, final CloseableHttpClient client, UserRepresentation user, RedirectStrategyWithSwitchableFollowRedirect strategy) { try {//ww w. j a va 2s . co m HttpClientContext context = HttpClientContext.create(); response = client.execute(post, context); String loginPageText = EntityUtils.toString(response.getEntity(), "UTF-8"); response.close(); HttpUriRequest loginRequest = LoginBuilder.handleLoginPage(user, loginPageText); strategy.setRedirectable(false); response = client.execute(loginRequest, context); response.close(); } catch (Exception ex) { throw new RuntimeException(ex); } finally { if (response != null) { EntityUtils.consumeQuietly(response.getEntity()); try { response.close(); } catch (IOException ex) { } } } }
From source file:org.owasp.benchmark.tools.BenchmarkCrawler.java
public static void sendPost(CloseableHttpClient httpclient, HttpPost post) throws Exception { CloseableHttpResponse response = httpclient.execute(post); System.out.println("POST " + post.getURI()); try {//from w w w . ja v a 2 s . co m HttpEntity entity = response.getEntity(); System.out.println("--> (" + response.getStatusLine().getStatusCode() + ") "); EntityUtils.consume(entity); } finally { response.close(); } }