List of usage examples for org.apache.http.impl.client CloseableHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:org.smartloli.kafka.eagle.common.util.HttpClientUtils.java
/** * Send request by get method.//from ww w. j av 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:com.hy.utils.pay.wx.ClientCustomSSL.java
public static void test() throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("D:/10016225.p12")); try {/*from w w w. j av a2s. c o m*/ keyStore.load(instream, "10016225".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "10016225".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 { HttpGet httpget = new HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.ibm.streamsx.topology.internal.streaminganalytics.RestUtils.java
public static JsonObject getGsonResponse(CloseableHttpClient httpClient, HttpRequestBase request) throws IOException, ClientProtocolException { request.addHeader("accept", ContentType.APPLICATION_JSON.getMimeType()); CloseableHttpResponse response = httpClient.execute(request); JsonObject jsonResponse;//from w w w. j av a 2 s . co m try { HttpEntity entity = response.getEntity(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { final String errorInfo; if (entity != null) errorInfo = " -- " + EntityUtils.toString(entity); else errorInfo = ""; throw new IllegalStateException( "Unexpected HTTP resource from service:" + response.getStatusLine().getStatusCode() + ":" + response.getStatusLine().getReasonPhrase() + errorInfo); } if (entity == null) throw new IllegalStateException("No HTTP resource from service"); Reader r = new InputStreamReader(entity.getContent()); jsonResponse = new Gson().fromJson(r, JsonObject.class); EntityUtils.consume(entity); } finally { response.close(); } return jsonResponse; }
From source file:com.ibm.mobilefirstplatform.serversdk.java.push.PushNotifications.java
protected static void executePushPostRequest(HttpPost pushPost, CloseableHttpClient httpClient, PushNotificationsResponseListener listener) { CloseableHttpResponse response = null; try {/* w w w. ja v a 2s. com*/ response = httpClient.execute(pushPost); if (listener != null) { sendResponseToListener(response, listener); } } catch (ClientProtocolException e) { logger.log(Level.SEVERE, e.toString(), e); if (listener != null) { listener.onFailure(null, null, e); } } catch (IOException e) { logger.log(Level.SEVERE, e.toString(), e); if (listener != null) { listener.onFailure(null, null, e); } } finally { if (response != null) { try { response.close(); } catch (IOException e) { // Closing response is merely a best effort. } } } }
From source file:com.ibm.watson.movieapp.dialog.rest.UtilityFunctions.java
/** * Makes HTTP GET request//w w w. j av a 2 s. c o m * <p> * This makes HTTP GET requests to the url provided. * * @param httpClient the http client used to make the request * @param uri the uri for the request * @return the JSON object response * @throws ClientProtocolException if it is unable to execute the call * @throws IOException if it is unable to execute the call * @throws IllegalStateException if the input stream could not be parsed correctly * @throws HttpException if the HTTP call responded with a status code other than 200 or 201 */ public static JsonObject httpGet(CloseableHttpClient httpClient, URI uri) throws ClientProtocolException, IOException, IllegalStateException, HttpException { HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { return UtilityFunctions.parseHTTPResponse(response, uri.toString()); } catch (ClientProtocolException e) { throw e; } }
From source file:com.android.tools.idea.stats.UsageTrackerAnalyticsImpl.java
/** * Send a ping to Analytics on a separate thread *//* w w w . j a v a 2 s .c o m*/ private static void sendPing(@NotNull final List<? extends NameValuePair> postData) { ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { @Override public void run() { CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost request = new HttpPost(ANALYTICS_URL); try { request.setEntity(new UrlEncodedFormEntity(postData)); HttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); HttpEntity entity = response.getEntity(); // throw it away, don't care, not sure if we need to read in the response? if (status.getStatusCode() >= 300) { LOG.debug("Non 200 status code : " + status.getStatusCode() + " - " + status.getReasonPhrase()); // something went wrong, fail quietly, we probably have to diagnose analytics errors on our side // usually analytics accepts ANY request and always returns 200 } } catch (IOException e) { LOG.debug("IOException during Analytics Ping", e.getMessage()); // something went wrong, fail quietly } finally { HttpClientUtils.closeQuietly(client); } } }); }
From source file:de.xwic.appkit.core.remote.client.URemoteAccessClient.java
/** * @param param/* ww w. j a va2 s . co m*/ * @param config * @return */ public static byte[] postRequest(final IRequestHelper helper, final RemoteSystemConfiguration config) { CloseableHttpResponse response = null; try { CloseableHttpClient client = PoolingHttpConnectionManager.getInstance().getClientInstance(config); HttpPost post = new HttpPost(helper.getTargetUrl()); post.setEntity(helper.getHttpEntity()); response = client.execute(post); int responseCode = response.getStatusLine().getStatusCode(); if (responseCode != HttpURLConnection.HTTP_OK) { throw new RemoteDataAccessException(response.getStatusLine().getReasonPhrase()); } return EntityUtils.toByteArray(response.getEntity()); } catch (RemoteDataAccessException re) { throw re; } catch (Exception e) { throw new RemoteDataAccessException(e); } finally { if (response != null) { try { response.close(); } catch (IOException e) { log.error(e.getMessage(), e); } } } }
From source file:org.apache.solr.cloud.TestMiniSolrCloudClusterSSL.java
/** * Trivial helper method for doing a HEAD request of the specified URL using the specified client * and getting the HTTP statusCode from the response *///from w w w.j av a 2s . co m private static int doHeadRequest(final CloseableHttpClient client, final String url) throws Exception { return client.execute(new HttpHead(url)).getStatusLine().getStatusCode(); }
From source file:com.google.gct.stats.GoogleUsageTracker.java
private static void sendPing(@NotNull final List<? extends NameValuePair> postData) { ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { public void run() { CloseableHttpClient client = HttpClientBuilder.create().build(); HttpPost request = new HttpPost(ANALYTICS_URL); try { request.setEntity(new UrlEncodedFormEntity(postData)); CloseableHttpResponse response = client.execute(request); StatusLine status = response.getStatusLine(); if (status.getStatusCode() >= 300) { LOG.debug("Non 200 status code : " + status.getStatusCode() + " - " + status.getReasonPhrase()); }/*w w w. j a va 2 s .c o m*/ } catch (IOException ex) { LOG.debug("IOException during Analytics Ping", new Object[] { ex.getMessage() }); } finally { HttpClientUtils.closeQuietly(client); } } }); }
From source file:com.oakhole.voa.utils.HttpClientUtils.java
/** * ???map// w ww . j a v a 2 s.c om * * @param uri * @return */ public static HttpEntity get(String uri) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(uri); try { CloseableHttpResponse httpResponse = httpClient.execute(httpGet); return httpResponse.getEntity(); } catch (IOException e) { logger.error(":{}", e.getMessage()); } return null; }