List of usage examples for org.apache.http.impl.client CloseableHttpClient execute
public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException
From source file:com.github.tomakehurst.wiremock.HttpsAcceptanceTest.java
static String secureContentFor(String url, String clientTrustStore, String trustStorePassword) throws Exception { KeyStore trustStore = readKeyStore(clientTrustStore, trustStorePassword); // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadKeyMaterial(trustStore, trustStorePassword.toCharArray()).useTLS().build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, // supported protocols null, // supported cipher suites SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); HttpGet get = new HttpGet(url); HttpResponse response = httpClient.execute(get); String content = EntityUtils.toString(response.getEntity()); return content; }
From source file:org.sead.repositories.reference.util.SEADGoogleLogin.java
static void getTokenFromRefreshToken() { access_token = null;/*w w w. j a v a 2s.co m*/ expires_in = -1; token_start_time = -1; if (gProps == null) { initGProps(); } /* Try refresh token */ // Query for token now that user has gone through browser part // of // flow // The method used in getTokensFromCode should work here as well - I // think URL encoded Form is the recommended way... HttpPost post = new HttpPost(gProps.token_uri); post.addHeader("accept", "application/json"); post.addHeader("Content-Type", "application/x-www-form-urlencoded"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); nameValuePairs.add(new BasicNameValuePair("client_id", gProps.client_id)); nameValuePairs.add(new BasicNameValuePair("client_secret", gProps.client_secret)); nameValuePairs.add(new BasicNameValuePair("refresh_token", refresh_token)); nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token")); try { post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; try { try { response = httpclient.execute(post); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { String responseJSON = EntityUtils.toString(resEntity); ObjectNode root = (ObjectNode) new ObjectMapper().readTree(responseJSON); access_token = root.get("access_token").asText(); // refresh_token = // root.get("refresh_token").asText(); token_start_time = System.currentTimeMillis() / 1000; expires_in = root.get("expires_in").asInt(); } } else { log.error("Error response from Google: " + response.getStatusLine().getReasonPhrase()); HttpEntity resEntity = response.getEntity(); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { log.error("Error obtaining access token: " + e.getMessage()); } finally { if (response != null) { response.close(); } httpclient.close(); } } catch (IOException io) { log.error("Error closing connections: " + io.getMessage()); } }
From source file:io.personium.plugin.base.utils.PluginUtils.java
/** * HTTP?JSON???. Cache?????????./* w w w .j a v a 2s . co m*/ * * @param url URL * @return JSONObject * @throws IOException IOException * @throws ClientProtocolException ClientProtocolException * @throws ParseException ParseException */ public static JSONObject getHttpJSON(String url) throws ClientProtocolException, IOException, ParseException { HttpGet get = new HttpGet(url); HttpResponse res = null; CloseableHttpClient httpProxyClient = null; // Connection Host if (ProxyUtils.isProxyHost()) { httpProxyClient = ProxyUtils.proxyHttpClient(); get.setConfig(ProxyUtils.getRequestConfig()); res = httpProxyClient.execute(get); } else { res = httpClient.execute(get); } InputStream is = res.getEntity().getContent(); String body = PluginUtils.readInputStreamAsString(is); JSONObject jsonObj = (JSONObject) new JSONParser().parse(body); return jsonObj; }
From source file:co.aurasphere.botmill.skype.util.network.NetworkUtils.java
/** * Send./* w w w . j a v a2 s . c o m*/ * * @param request the request * @return the string */ private static String send(HttpRequestBase request) { CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(null, null); provider.setCredentials(AuthScope.ANY, credentials); CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); logger.debug(request.getRequestLine().toString()); HttpResponse httpResponse = null; String response = null; try { httpResponse = httpClient.execute(request); response = logResponse(httpResponse); } catch (Exception e) { logger.error("Error during HTTP connection to Kik: ", e); } finally { try { httpClient.close(); } catch (IOException e) { logger.error("Error while closing HTTP connection: ", e); } } return response; }
From source file:co.aurasphere.botmill.telegram.internal.util.network.NetworkUtils.java
/** * Sends a request.//from www . j av a 2 s.c o m * * @param request * the request to send * @return response the response. */ private static String send(HttpRequestBase request) { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); logger.debug(request.getRequestLine().toString()); HttpResponse httpResponse = null; String response = null; try { httpResponse = httpClient.execute(request); response = logResponse(httpResponse); } catch (Exception e) { logger.error("Error during HTTP connection to Telegram: ", e); } finally { try { httpClient.close(); } catch (IOException e) { logger.error("Error while closing HTTP connection: ", e); } } return response; }
From source file:org.aludratest.cloud.selenium.impl.SeleniumResourceImpl.java
private static void performDelete(String url) { CloseableHttpClient client = HttpClientBuilder.create().build(); try {// w ww.j a v a 2 s . co m HttpDelete request = new HttpDelete(url); client.execute(request); } catch (IOException e) { // ignore silently LOG.debug("Could not execute a DELETE on url " + url, e); } finally { IOUtils.closeQuietly(client); } }
From source file:myexamples.dbase.MyHelpMethods.java
public static String sendHttpRequest2(String url, String requestBody, String requestType) throws IOException { String result = null;/*from ww w .j av a 2s .co m*/ CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse httpResponse; try { switch (requestType) { case "Get": httpResponse = httpclient.execute(new HttpGet(url)); break; case "Post": HttpPost httppost = new HttpPost(url); if (requestBody != null) httppost.setEntity(new StringEntity(requestBody)); httpResponse = httpclient.execute(httppost); break; case "Put": HttpPut httpPut = new HttpPut(url); httpPut.addHeader("Content-Type", "application/json"); httpPut.addHeader("Accept", "application/json"); if (requestBody != null) httpPut.setEntity(new StringEntity(requestBody, DEFAULT_CHARSET)); httpResponse = httpclient.execute(httpPut); break; case "Delete": httpResponse = httpclient.execute(new HttpDelete(url)); break; default: httpResponse = httpclient.execute(new HttpGet(url)); break; } try { HttpEntity entity1 = httpResponse.getEntity(); if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < MAX_CONTENT_LENGTH) { result = EntityUtils.toString(entity1, DEFAULT_CHARSET); } else { System.out.println("Error!!!! entity length=" + len); } } EntityUtils.consume(entity1); } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { httpclient.close(); } return result; }
From source file:co.aurasphere.botmill.util.NetworkUtils.java
/** * Send./*w ww.ja v a 2s . c o m*/ * * @param request * the request * @return the string */ private static String send(HttpRequestBase request) { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); logger.debug(request.getRequestLine().toString()); HttpResponse httpResponse = null; String response = null; try { httpResponse = httpClient.execute(request); response = logResponse(httpResponse); } catch (Exception e) { logger.error("Error during HTTP connection to RASA: ", e); } finally { try { httpClient.close(); } catch (IOException e) { logger.error("Error while closing HTTP connection: ", e); } } return response; }
From source file:com.srotya.tau.ui.BapiLoginDAO.java
public static Entry<String, String> authenticate(String authURL, String username, String password) throws Exception { CloseableHttpClient client = Utils.buildClient(authURL, 3000, 5000); HttpPost authRequest = new HttpPost(authURL); Gson gson = new Gson(); JsonObject obj = new JsonObject(); obj.addProperty(USERNAME, username); obj.addProperty(PASSWORD, password); StringEntity entity = new StringEntity(gson.toJson(obj), ContentType.APPLICATION_JSON); authRequest.setEntity(entity);/*from w ww . java 2s .com*/ CloseableHttpResponse response = client.execute(authRequest); if (response.getStatusLine().getStatusCode() == 200) { String tokenPair = EntityUtils.toString(response.getEntity()); JsonArray ary = gson.fromJson(tokenPair, JsonArray.class); obj = ary.get(0).getAsJsonObject(); String token = obj.get(X_SUBJECT_TOKEN).getAsString(); String hmac = obj.get(HMAC).getAsString(); return new AbstractMap.SimpleEntry<String, String>(token, hmac); } else { System.err.println("Login failed:" + response.getStatusLine().getStatusCode() + "\t" + response.getStatusLine().getReasonPhrase()); return null; } }
From source file:com.oakhole.voa.utils.HttpClientUtils.java
/** * ??//from w w w.j a v a 2 s.c o m * * @param uri * @param params * @return */ public static String post(String uri, String params) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(uri); StringEntity stringEntity = new StringEntity(params, CHARSET.toString()); httpPost.setEntity(stringEntity); try { CloseableHttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { httpResponse.close(); return EntityUtils.toString(httpResponse.getEntity(), CHARSET); } } catch (IOException e) { logger.error(":{}", e.getMessage()); } return ""; }