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.wuspba.ctams.ws.ITBandResultController.java
protected static void delete() throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("id", TestFixture.INSTANCE.bandResult.getId()).build(); HttpDelete httpDelete = new HttpDelete(uri); CloseableHttpResponse response = null; try {// www .j av a 2s .c o m response = httpclient.execute(httpDelete); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } ITBandController.delete(); ITBandContestController.delete(); }
From source file:org.wuspba.ctams.ws.ITSoloContestController.java
protected static void delete() throws Exception { String id;// w w w . j av a 2s . co m CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); id = doc.getSoloContests().get(0).getId(); EntityUtils.consume(entity); } httpclient = HttpClients.createDefault(); uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).setParameter("id", id) .build(); HttpDelete httpDelete = new HttpDelete(uri); CloseableHttpResponse response = null; try { response = httpclient.execute(httpDelete); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } ITVenueController.delete(); ITHiredJudgeController.delete(); ITJudgeController.delete(); }
From source file:com.deying.util.weixin.ClientCustomSSL.java
public static String doRefund(String url, String data) throws Exception { System.out.println("111111111111111111111111111111111111111111111118 "); KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("E:/apiclient_cert.p12"));//P12 try {//from w ww. ja v a 2 s .c o m keyStore.load(instream, "1233374102".toCharArray());//?..MCHID } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1233374102".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:emea.summit.architects.HackathlonAPIResource.java
private static String postRequest(String url, String contentType, HttpEntity entity) throws Exception { HttpClientBuilder builder = HttpClientBuilder.create(); CloseableHttpClient client = builder.build(); HttpPost post = new HttpPost(url); post.setHeader("Content-Type", contentType); post.setEntity(entity);//w w w .j av a 2 s. c o m HttpResponse response = client.execute(post); StatusLine status = response.getStatusLine(); String content = EntityUtils.toString(response.getEntity()); //JSONObject json = new JSONObject(content); return content; }
From source file:org.wuspba.ctams.ws.ITBandController.java
private static void add(Band band) throws Exception { CTAMSDocument doc = new CTAMSDocument(); doc.getBands().add(band);/*from ww w. java 2s. c om*/ String xml = XMLUtils.marshal(doc); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpPost httpPost = new HttpPost(uri); StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML); CloseableHttpResponse response = null; try { httpPost.setEntity(xmlEntity); response = httpclient.execute(httpPost); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); doc = IntegrationTestUtils.convertEntity(responseEntity); TestFixture.INSTANCE.skye.setId(doc.getBands().get(0).getId()); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } }
From source file:org.dashbuilder.dataprovider.backend.elasticsearch.ElasticSearchDataSetTestBase.java
/** * <p>Index documents in bulk mode into EL server.</p> *///from ww w . j a v a 2 s.c o m protected static void addDocuments(CloseableHttpClient httpClient, File dataContentFile) throws Exception { HttpPost httpPost2 = new HttpPost(urlBuilder.getBulk()); FileEntity inputData = new FileEntity(dataContentFile); inputData.setContentType(CONTENTTYPE_JSON); httpPost2.addHeader(HEADER_ACCEPT, CONTENTTYPE_JSON); httpPost2.addHeader(HEADER_CONTENTTYPE, CONTENTTYPE_JSON); httpPost2.setEntity(inputData); CloseableHttpResponse dataResponse = httpClient.execute(httpPost2); if (dataResponse.getStatusLine().getStatusCode() != EL_REST_RESPONSE_OK) { System.out.println("Error response body:"); System.out.println(responseAsString(dataResponse)); } httpPost2.completed(); Assert.assertEquals(dataResponse.getStatusLine().getStatusCode(), EL_REST_RESPONSE_OK); }
From source file:org.wuspba.ctams.ws.ITRosterController.java
protected static void delete() throws Exception { List<String> ids = new ArrayList<>(); CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build(); HttpGet httpGet = new HttpGet(uri); try (CloseableHttpResponse response = httpclient.execute(httpGet)) { assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING); HttpEntity entity = response.getEntity(); CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity); for (Roster r : doc.getRosters()) { ids.add(r.getId());/*from w w w .j av a 2 s . co m*/ } EntityUtils.consume(entity); } for (String id : ids) { httpclient = HttpClients.createDefault(); uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("id", id).build(); HttpDelete httpDelete = new HttpDelete(uri); CloseableHttpResponse response = null; try { response = httpclient.execute(httpDelete); assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString()); HttpEntity responseEntity = response.getEntity(); EntityUtils.consume(responseEntity); } catch (UnsupportedEncodingException ex) { LOG.error("Unsupported coding", ex); } catch (IOException ioex) { LOG.error("IOException", ioex); } finally { if (response != null) { try { response.close(); } catch (IOException ex) { LOG.error("Could not close response", ex); } } } } ITBandRegistrationController.delete(); ITBandMemberController.delete(); }
From source file:com.bbc.util.ClientCustomSSL.java
public static String clientCustomSLL(String mchid, String path, String data) throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); System.out.println("?..."); FileInputStream instream = new FileInputStream(new File("/payment/apiclient_cert.p12")); try {//from w w w . java2 s . com keyStore.load(instream, mchid.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchid.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(path); 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(); 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; StringBuffer sb = new StringBuffer(""); while ((text = bufferedReader.readLine()) != null) { System.out.println(text); sb.append(text); } return sb.toString(); } EntityUtils.consume(entity); return ""; } finally { response.close(); } } finally { httpclient.close(); } }
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 w w.j a va2s. 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:org.pepstock.jem.commands.util.HttpUtil.java
/** * Calls a http node of JEM to get all members of group, necessary to client * to connect to JEM.//w w w. ja v a 2 s . c o m * * @param url http URL to call * @return Arrays with all members of Hazelcast cluster * @throws SubmitException if errors occur */ public static String[] getMembers(String url) throws SubmitException { // creates a HTTP client CloseableHttpClient httpclient = null; try { httpclient = createHttpClient(url); // concats URL with query string String completeUrl = url + HttpUtil.MEMBERS_QUERY_STRING; // prepares GET request and basic response handler HttpGet httpget = new HttpGet(completeUrl); CloseableHttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); if (entity != null) { long len = entity.getContentLength(); if (len != -1 && len < 2048) { // executes and parse the results // result must be // [ipaddress:port],[ipaddress:port],[ipaddress:port],....[ipaddress:port] return EntityUtils.toString(entity).trim().split(","); } else { throw new IOException("HTTP Entity content length wrong: " + len); } } throw new IOException("HTTP Entity is null"); } catch (KeyManagementException e) { throw new SubmitException(SubmitMessage.JEMW001E, e); } catch (UnrecoverableKeyException e) { throw new SubmitException(SubmitMessage.JEMW001E, e); } catch (NoSuchAlgorithmException e) { throw new SubmitException(SubmitMessage.JEMW001E, e); } catch (KeyStoreException e) { throw new SubmitException(SubmitMessage.JEMW001E, e); } catch (URISyntaxException e) { throw new SubmitException(SubmitMessage.JEMW001E, e); } catch (ClientProtocolException e) { throw new SubmitException(SubmitMessage.JEMW001E, e); } catch (IOException e) { throw new SubmitException(SubmitMessage.JEMW001E, e); } finally { // close http client if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { LogAppl.getInstance().ignore(e.getMessage(), e); } } } }