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.huotu.mallduobao.common.thirdparty.ClientCustomSSL.java
public static String doRefund(String url, String data, String celPath, String celPassword) throws Exception { /**/*from w w w .ja va2s .c o m*/ * ?PKCS12? ?-- API */ KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(celPath));//P12 try { /** * ? * */ keyStore.load(instream, celPassword.toCharArray());//?..MCHID } finally { instream.close(); } // Trust own CA and all self-signed certs /** * ? * */ SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, celPassword.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:com.simple.weixin.refund.ClientCustomSSL.java
public static String doRefund(String password, String keyStrore, String url, String data) throws Exception { /**/* ww w . j a va2 s .co m*/ * ?PKCS12? ?-- API */ KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(keyStrore));//P12 try { /** * ? * */ keyStore.load(instream, password.toCharArray());//?..MCHID } finally { instream.close(); } // Trust own CA and all self-signed certs /** * ? * */ SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password.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:com.zxy.commons.httpclient.HttpclientUtils.java
/** * url?/*w w w . j a va 2 s . com*/ * * @param connectTimeoutSec () * @param socketTimeoutSec ??() * @param url url? * @param outputFile ? * @return ?? * @throws ClientProtocolException ClientProtocolException * @throws IOException IOException */ public static boolean download(int connectTimeoutSec, int socketTimeoutSec, String url, String outputFile) throws ClientProtocolException, IOException { CloseableHttpClient httpClient = null; CloseableHttpResponse httpResponse = null; InputStream in = null; OutputStream out = null; try { // httpClient = HttpClients.createDefault(); RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeoutSec * 1000) .setConnectionRequestTimeout(connectTimeoutSec * 1000).setSocketTimeout(socketTimeoutSec * 1000) .build(); httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); HttpGet httpGet = new HttpGet(url); httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); in = entity.getContent(); File file = new File(outputFile); out = new FileOutputStream(file); IOUtils.copy(in, out); return file.exists(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); HttpClientUtils.closeQuietly(httpResponse); HttpClientUtils.closeQuietly(httpClient); } }
From source file:org.wuspba.ctams.ws.ITSoloResultController.java
private static void add() throws Exception { ITSoloContestController.add();/* w ww . j av a 2 s. c o m*/ CTAMSDocument doc = new CTAMSDocument(); doc.getPeople().add(TestFixture.INSTANCE.elaine); doc.getVenues().add(TestFixture.INSTANCE.venue); doc.getJudges().add(TestFixture.INSTANCE.judgeAndy); doc.getJudges().add(TestFixture.INSTANCE.judgeJamie); doc.getJudges().add(TestFixture.INSTANCE.judgeBob); doc.getResults().add(TestFixture.INSTANCE.result5); doc.getSoloContests().add(TestFixture.INSTANCE.soloContest); doc.getSoloContestResults().add(TestFixture.INSTANCE.soloResult); 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.soloResult.setId(doc.getSoloContestResults().get(0).getId()); for (Result r : doc.getSoloContestResults().get(0).getResults()) { if (r.getPoints() == TestFixture.INSTANCE.result1.getPoints()) { TestFixture.INSTANCE.result1.setId(r.getId()); } else if (r.getPoints() == TestFixture.INSTANCE.result2.getPoints()) { TestFixture.INSTANCE.result2.setId(r.getId()); } else if (r.getPoints() == TestFixture.INSTANCE.result3.getPoints()) { TestFixture.INSTANCE.result3.setId(r.getId()); } else if (r.getPoints() == TestFixture.INSTANCE.result4.getPoints()) { TestFixture.INSTANCE.result4.setId(r.getId()); } else if (r.getPoints() == TestFixture.INSTANCE.result5.getPoints()) { TestFixture.INSTANCE.result5.setId(r.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:com.zxy.commons.httpclient.HttpclientUtils.java
/** * url?byte[]//from ww w.j a v a2s. co m * * @param connectTimeoutSec () * @param socketTimeoutSec ??() * @param url url? * @return byte[] * @throws IOException IOException */ public static byte[] download(int connectTimeoutSec, int socketTimeoutSec, String url) throws IOException { CloseableHttpClient httpClient = null; CloseableHttpResponse httpResponse = null; InputStream in = null; ByteArrayOutputStream out = null; try { // httpClient = HttpClients.createDefault(); RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeoutSec * 1000) .setConnectionRequestTimeout(connectTimeoutSec * 1000).setSocketTimeout(socketTimeoutSec * 1000) .build(); httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); HttpGet httpGet = new HttpGet(url); httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); in = entity.getContent(); out = new ByteArrayOutputStream(); IOUtils.copy(in, out); return out.toByteArray(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); HttpClientUtils.closeQuietly(httpResponse); HttpClientUtils.closeQuietly(httpClient); } }
From source file:cn.digirun.frame.payment.wxpay.util.ClientCustomSSL.java
public static String doRefund(String url, String data) throws Exception { /**/*from ww w .ja va 2 s.c om*/ * ?PKCS12? ?-- API */ KeyStore keyStore = KeyStore.getInstance("PKCS12"); /** * ? */ //ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+ ""); // FileInputStream instream = new FileInputStream(new File("D:/Program Files/MyEclipse 6.5/workspace/weidian/WebRoot/cer/apiclient_cert.p12"));//P12 FileInputStream instream = new FileInputStream( ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + WxpayConfig.cert_path)); try { /** * ? * MCHID * */ keyStore.load(instream, WxpayConfig.mch_id.toCharArray()); } finally { instream.close(); } SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, WxpayConfig.mch_id.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:eu.diacron.crawlservice.app.Util.java
public static String getCrawlStatusById(String crawlid) { String status = ""; System.out.println("get crawlid"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME, Configuration.REMOTE_CRAWLER_PASS)); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try {// www. ja v a 2 s.c o m //HttpGet httpget = new HttpGet("http://diachron.hanzoarchives.com/crawl/" + crawlid); HttpGet httpget = new HttpGet(Configuration.REMOTE_CRAWLER_URL_CRAWL + crawlid); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println("----------------------------------------"); String result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); result += inputLine; } in.close(); // TO-DO should be removed in the future and handle it more gracefully result = result.replace("u'", "'"); result = result.replace("'", "\""); JSONObject crawljson = new JSONObject(result); System.out.println("myObject " + crawljson.toString()); status = crawljson.getString("status"); EntityUtils.consume(response.getEntity()); } catch (JSONException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } finally { response.close(); } } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } finally { try { httpclient.close(); } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } } return status; }
From source file:org.dashbuilder.dataprovider.backend.elasticsearch.ElasticSearchDataSetTestBase.java
/** * <p>Index a single document into EL server.</p> *///from w w w . ja v a 2 s. co m protected static void addDocument(CloseableHttpClient httpclient, String type, String document) throws Exception { HttpPost httpPut = new HttpPost(urlBuilder.getIndexRoot() + "/" + type); StringEntity inputData = new StringEntity(document); inputData.setContentType(CONTENTTYPE_JSON); httpPut.addHeader(HEADER_ACCEPT, CONTENTTYPE_JSON); httpPut.addHeader(HEADER_CONTENTTYPE, CONTENTTYPE_JSON); httpPut.setEntity(inputData); CloseableHttpResponse dataResponse = httpclient.execute(httpPut); if (dataResponse.getStatusLine().getStatusCode() != EL_REST_RESPONSE_CREATED) { System.out.println("Error response body:"); System.out.println(responseAsString(dataResponse)); } Assert.assertEquals(dataResponse.getStatusLine().getStatusCode(), EL_REST_RESPONSE_CREATED); }
From source file:eu.diacron.crawlservice.app.Util.java
public static JSONArray getwarcsByCrawlid(String crawlid) { JSONArray warcsArray = null;/*from w w w. j a v a 2s .co m*/ System.out.println("get crawlid"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); /* credsProvider.setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("diachron", "7nD9dNGshTtficn")); */ credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME, Configuration.REMOTE_CRAWLER_PASS)); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); try { //HttpGet httpget = new HttpGet("http://diachron.hanzoarchives.com/warcs/" + crawlid); HttpGet httpget = new HttpGet(Configuration.REMOTE_CRAWLER_URL + crawlid); System.out.println("Executing request " + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); System.out.println("----------------------------------------"); String result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); result += inputLine; } in.close(); result = result.replace("u'", "'"); result = result.replace("'", "\""); warcsArray = new JSONArray(result); for (int i = 0; i < warcsArray.length(); i++) { System.out.println("url to download: " + warcsArray.getString(i)); } EntityUtils.consume(response.getEntity()); } catch (JSONException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } finally { response.close(); } } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } finally { try { httpclient.close(); } catch (IOException ex) { Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex); } } return warcsArray; }
From source file:org.wuspba.ctams.ws.ITPersonController.java
public static Person getPerson(String firstName, String lastName) throws Exception { Person ret;// w ww .j av a 2 s . co m CloseableHttpClient httpclient = HttpClients.createDefault(); URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH) .setParameter("firstname", firstName).setParameter("lastname", lastName).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); assertEquals(doc.getPeople().size(), 1); ret = doc.getPeople().get(0); EntityUtils.consume(entity); } return ret; }