List of usage examples for org.apache.http.impl.client CloseableHttpClient close
public void close() throws IOException;
From source file:com.calmio.calm.integration.Helpers.HTTPHandler.java
public static HTTPResponseData sendGet(String url, String username, String pwd) throws Exception { CloseableHttpClient client = HttpClients.custom() .setDefaultCredentialsProvider(getCredentialsProvider(url, username, pwd)).build(); int responseCode = 0; StringBuffer respo = null;/* w ww . jav a 2 s .c o m*/ String userPassword = username + ":" + pwd; // String encoding = Base64.getEncoder().encodeToString(userPassword.getBytes()); String encoding = Base64.encodeBase64String(userPassword.getBytes()); try { HttpGet request = new HttpGet(url); request.addHeader("Authorization", "Basic " + encoding); request.addHeader("User-Agent", USER_AGENT); System.out.println("Executing request " + request.getRequestLine()); CloseableHttpResponse response = client.execute(request); try { responseCode = response.getStatusLine().getStatusCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); respo = new StringBuffer(); String inputLine; while ((inputLine = in.readLine()) != null) { respo.append(inputLine); } } finally { response.close(); } } finally { client.close(); } HTTPResponseData result = new HTTPResponseData(responseCode, ((respo == null) ? "" : respo.toString())); System.out.println(result.getStatusCode() + "/n" + result.getBody()); return result; }
From source file:com.kingmed.dp.ndp.NDPCommunicator.java
public void singOut() { String urlForSignOut = ndpServe.getUrlSignout(); CloseableHttpClient httpclient = HttpClients.createDefault(); SignInResponseHandler responseHandler = new SignInResponseHandler(); try {/*from w ww. j av a2 s . co m*/ HttpGet httpget = new HttpGet(urlForSignOut); log.info(" " + httpget.getRequestLine()); httpclient.execute(httpget, responseHandler); cookie = responseHandler.getCookie(); } catch (Exception e) { log.error("", e); } finally { try { httpclient.close(); } catch (Exception e) { log.error("http", e); } } }
From source file:com.kingmed.dp.ndp.NDPCommunicator.java
/** * //from ww w. j a v a2s . c o m * * @return cookie ?cookie */ public String signin() { String urlForSignIn = ndpServe.getUrlSignin(); CloseableHttpClient httpclient = HttpClients.createDefault(); SignInResponseHandler responseHandler = new SignInResponseHandler(); try { HttpGet httpget = new HttpGet(urlForSignIn); log.info(" " + httpget.getRequestLine()); httpclient.execute(httpget, responseHandler); cookie = responseHandler.getCookie(); } catch (Exception e) { log.error("?", e); } finally { try { httpclient.close(); } catch (Exception e) { log.error("http", e); } } return null; }
From source file:org.cloudsimulator.utility.RestAPI.java
public static ResponseMessageByteArray receiveByteArray(final String restAPIURI, final String username, final String password, final String typeOfByteArray) throws IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse httpResponse = null; ResponseMessageByteArray responseMessageByteArray = null; httpResponse = getRequestBasicAuth(httpClient, escapeURI(restAPIURI), username, password, typeOfByteArray); if (httpResponse != null) { if (httpResponse.getStatusLine() != null) { if (httpResponse.getEntity() != null) { responseMessageByteArray = new ResponseMessageByteArray( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase(), IOUtils.toByteArray(httpResponse.getEntity().getContent())); } else { responseMessageByteArray = new ResponseMessageByteArray( httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase(), null); }/*from ww w . j a v a 2 s . c o m*/ } else { if (httpResponse.getEntity() != null) { responseMessageByteArray = new ResponseMessageByteArray(null, null, IOUtils.toByteArray(httpResponse.getEntity().getContent())); } } httpResponse.close(); } httpClient.close(); return responseMessageByteArray; }
From source file:Vdisk.java
public JSONObject vdisk_get_operation(URI uri) throws IOException { CloseableHttpClient getClient = HttpClients.createDefault(); if (access_token == null) this.get_access_token(); HttpGet httpGet = new HttpGet(uri); JSONObject res = null;/*ww w . ja v a 2 s .co m*/ try (CloseableHttpResponse response = getClient.execute(httpGet)) { HttpEntity entity = response.getEntity(); String info = EntityUtils.toString(entity); res = JSONObject.fromObject(info); } finally { getClient.close(); } return res; }
From source file:de.zazaz.iot.bosch.indego.ifttt.IftttIndegoAdapter.java
/** * Closes the HTTP client./*from ww w . j a v a2s .c om*/ * * @param httpClient the HTTP client instance to close */ private void disconnect(CloseableHttpClient httpClient) { try { if (httpClient != null) { LOG.info("Closing the HTTP client"); httpClient.close(); } } catch (Exception ex) { LOG.warn("Something strange happened while closing the HTTP client", ex); } }
From source file:Vdisk.java
public JSONObject vdisk_post_operation(URI uri) throws IOException { CloseableHttpClient postClient = HttpClients.createDefault(); if (access_token == null) this.get_access_token(); HttpPost httpPost = new HttpPost(uri); JSONObject res = null;/*from w w w. j a va 2s.c o m*/ try (CloseableHttpResponse response = postClient.execute(httpPost)) { HttpEntity entity = response.getEntity(); String info = EntityUtils.toString(entity); res = JSONObject.fromObject(info); } finally { postClient.close(); } return res; }
From source file:com.nibss.util.Request.java
public void post(String url, List<NameValuePair> list) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/* www.j a v a 2 s . c o m*/ HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(list)); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { System.out.println(response2.getStatusLine().getStatusCode()); HttpEntity entity2 = response2.getEntity(); EntityUtils.consume(entity2); } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:com.restfiddle.handler.http.GetHandler.java
public RfResponseDTO process(String apiUrl, String userName, String password, boolean useBasic64Auth) throws IOException { RfResponseDTO response = null;/*from www. j av a2 s. co m*/ CloseableHttpClient httpclient = null; BasicAuthHandler basicHttpAuthHandler = new BasicAuthHandler(); HttpGet httpRequest = new HttpGet(apiUrl); // TODO : Add auth logic. if (useBasic64Auth) { //httpclient = basicHttpAuthHandler.prepareBasicAuthWithBase64Encode(httpRequest, userName, password); } else { // httpclient = basicHttpAuthHandler.prepareBasicAuth(userName, password); } try { response = processHttpRequest(httpRequest, httpclient); } finally { httpclient.close(); } return response; }
From source file:fr.lissi.belilif.om2m.rest.WebServiceActions.java
/** * Do get.// w w w .ja v a2s . co m * * @param uri * the uri * @param headers * the headers * @return the string * @throws ClientProtocolException * the client protocol exception * @throws IOException * Signals that an I/O exception has occurred. * @throws HttpResponseException * the http response exception */ public static HttpGetSimpleResp doGet(String uri, HashMap<String, String> headers) throws ClientProtocolException, IOException, HttpResponseException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGetSimpleResp resp = new HttpGetSimpleResp(); try { HttpGet httpGet = new HttpGet(uri); for (String key : headers.keySet()) { httpGet.addHeader(key, headers.get(key)); } CloseableHttpResponse response = httpclient.execute(httpGet); resp.setStatusCode(response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { try { HttpEntity entity = response.getEntity(); if (entity != null) { // TODO to use for performance in the future // ResponseHandler<String> handler = new BasicResponseHandler(); resp.setResult(new BasicResponseHandler().handleResponse(response)); } EntityUtils.consume(entity); } finally { response.close(); } } else { // TODO optimiz (repeating code) throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()); } } finally { httpclient.close(); } return resp; }