List of usage examples for org.apache.http.client.methods CloseableHttpResponse close
public void close() throws IOException;
From source file:org.codehaus.mojo.license.utils.HttpRequester.java
/** * this method will send a simple GET-request to the given destination and will return the result as a * string/*from www. j a v a 2 s . c o m*/ * * @param url the resource destination that is expected to contain pure text * @return the string representation of the resource at the given URL */ public static String getFromUrl(String url) throws MojoExecutionException { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); HttpGet get = new HttpGet(url); CloseableHttpResponse response = null; String result = null; try { response = httpClient.execute(get); result = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8")); } catch (ClientProtocolException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } finally { if (response != null) { try { response.close(); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } } } return result; }
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;/*ww w . j a v a 2 s . 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:org.openo.nfvo.emsdriver.northbound.client.HttpClientUtil.java
public static String doDelete(String url, String charset) { CloseableHttpClient httpClient = null; HttpDelete httpDelete = null;/*from w w w . j av a 2 s . c om*/ String result = null; try { httpClient = HttpClientFactory.getSSLClientFactory(); httpDelete = new HttpDelete(url); CloseableHttpResponse response = httpClient.execute(httpDelete); 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("doDelete is fail ", e); } finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { } } } return result; }
From source file:org.jenkinsci.plugins.fabric8.support.RestClient.java
public static String parse(CloseableHttpResponse response) throws IOException { try {/* ww w. jav a2 s. com*/ BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder builder = new StringBuilder(); builder.append("status: " + response.getStatusLine()); String line = ""; while ((line = rd.readLine()) != null) { System.out.println(line); builder.append(line); builder.append("/n"); } return builder.toString(); } finally { response.close(); } }
From source file:org.openo.nfvo.emsdriver.northbound.client.HttpClientUtil.java
public static String doPost(String url, String json, String charset) { CloseableHttpClient httpClient = null; HttpPost httpPost = null;/*from w w w. ja v a 2s. c o m*/ String result = null; try { httpClient = HttpClientFactory.getSSLClientFactory(); httpPost = new HttpPost(url); if (null != json) { StringEntity s = new StringEntity(json); s.setContentEncoding("UTF-8"); s.setContentType("application/json"); // set contentType httpPost.setEntity(s); } CloseableHttpResponse response = httpClient.execute(httpPost); try { if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception e) { log.error("httpClient.execute(httpPost) is fail", e); } finally { if (response != null) { response.close(); } } } catch (Exception e) { log.error("doPost is fail ", e); } finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { } } } return result; }
From source file:cn.mrdear.pay.util.WebUtils.java
/** * ?,// w ww . j a v a 2 s . c om * @param httpResponse ?? * @return */ private static String consumeResponse(CloseableHttpResponse httpResponse) { String result = null; try { HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { result = EntityUtils.toString(httpEntity, "UTF-8"); EntityUtils.consume(httpEntity); } } catch (IOException e) { e.printStackTrace(); } finally { try { httpResponse.close(); } catch (IOException ignored) { } } return result; }
From source file:com.vmware.gemfire.tools.pulse.tests.junit.BaseServiceTest.java
/** * Logout to pulse server and close httpClient * To be called from setupAfterClass in each test class *///from www . j ava 2 s .co m protected static void doLogout() throws Exception { System.out.println("BaseServiceTest :: Executing doLogout with user : admin, password : admin."); if (httpclient != null) { CloseableHttpResponse logoutResponse = null; try { HttpUriRequest logout = RequestBuilder.get().setUri(new URI(LOGOUT_URL)).build(); logoutResponse = httpclient.execute(logout); try { HttpEntity entity = logoutResponse.getEntity(); EntityUtils.consume(entity); } finally { if (logoutResponse != null) logoutResponse.close(); httpclient.close(); httpclient = null; } } catch (Exception failed) { logException(failed); throw failed; } System.out.println("BaseServiceTest :: Executed doLogout"); } else { System.out.println("BaseServiceTest :: User NOT logged-in"); } }
From source file:com.caibowen.gplume.misc.test.HttpClientUtil.java
/** * http get//from w ww .java 2 s . c om * * @param url * @param params * @param connectTimeout * @return */ public static String get(String url, Map<String, String> params, int connectTimeout, Charset charset) { final String uri = setParam(url, params, charset); final HttpGet get = new HttpGet(uri); get.setConfig(buildConfig(connectTimeout, connectTimeout)); try { final CloseableHttpResponse response = httpClient.execute(get); try { final HttpEntity entity = response.getEntity(); if (entity != null) return EntityUtils.toString(entity, charset); } catch (Exception e) { logger.error(String.format("[HttpUtils Get] get response error, url:%s", uri), e); } finally { if (response != null) response.close(); } } catch (SocketTimeoutException e) { logger.error(String.format("[HttpUtils Get] invoke timeout error, url:%s", uri)); } catch (SocketException e) { logger.error(String.format("[HttpUtils Get] invoke connection refused, url:%s", uri)); } catch (Exception e) { logger.error(String.format("[HttpUtils Get] invoke error, url:%s", uri), e); } finally { get.releaseConnection(); } return null; }
From source file:com.networknt.light.server.handler.loader.MenuLoader.java
private static void loadMenuFile(File file) { Scanner scan = null;/*from w ww . j a v a2 s.c om*/ try { scan = new Scanner(file, Loader.encoding); // the content is only the data portion. convert to map String content = scan.useDelimiter("\\Z").next(); HttpPost httpPost = new HttpPost("http://injector:8080/api/rs"); StringEntity input = new StringEntity(content); input.setContentType("application/json"); httpPost.setEntity(input); CloseableHttpResponse response = httpclient.execute(httpPost); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); String json = ""; String line = ""; while ((line = rd.readLine()) != null) { json = json + line; } System.out.println("json = " + json); EntityUtils.consume(entity); } finally { response.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (scan != null) scan.close(); } }
From source file:com.cht.imserver.push.TLPushNotification.java
public static PNResult pushMessage_Android(String token, String message, String sender, String licenseKey, PNServerType type, String proxy, int port) throws IOException, UnsupportedEncodingException, ClientProtocolException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpHost httpproxy = null;/*w w w. java 2 s. c o m*/ String serverURL = null; PNResult result = null; if (type == PNServerType.android_dev) { serverURL = devAndroidHost; } else if (type == PNServerType.android_official) { serverURL = officialAndroidHost; } String jsontoken = "{\"androidTokens\":[{\"token\":\"" + token + "\"}]}"; String jsonmessage = "{\"sender\":\"" + sender + "\",\"message\":\"" + message + "\"}"; //System.out.println("androiddata=" + jsonmessage); //System.out.println("androidtoken=" + jsontoken); //logger.info("jsonmessage:" + jsonmessage + ", jsontoken:" + jsontoken + ", licenseKey:" + licenseKey ); logger.info("jsonmessage:" + jsonmessage + ", licenseKey:" + licenseKey); try { HttpPost httpPost = new HttpPost(serverURL); if (proxy != null) { httpproxy = new HttpHost(proxy, port); RequestConfig config = RequestConfig.custom().setProxy(httpproxy).build(); httpPost.setConfig(config); } List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("androiddata", jsonmessage)); nvps.add(new BasicNameValuePair("androidtoken", jsontoken)); nvps.add(new BasicNameValuePair("licensekey", licenseKey)); nvps.add(new BasicNameValuePair("appname", appname)); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); //System.out.println(EntityUtils.toString(httpPost.getEntity()) ); CloseableHttpResponse response2 = httpclient.execute(httpPost); Gson mGson = new Gson(); try { //System.out.println(response2.getStatusLine()); HttpEntity entity2 = response2.getEntity(); //System.out.println(EntityUtils.toString(entity2)); result = mGson.fromJson(EntityUtils.toString(entity2), PNResult.class); EntityUtils.consume(entity2); } finally { response2.close(); } } finally { httpclient.close(); } return result; }