List of usage examples for org.apache.http.client.methods CloseableHttpResponse getEntity
HttpEntity getEntity();
From source file:com.simple.weixin.refund.ClientCustomSSL.java
public static String doRefund(String password, String keyStrore, String url, String data) throws Exception { /**/* www .jav a 2 s . c o 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.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);/* w ww . ja v a 2s.c o m*/ 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:shootersubdownloader.Shootersubdownloader.java
private static void down(File f) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); String url = String.format("https://www.shooter.cn/api/subapi.php?filehash=%s&pathinfo=%s&format=json", computefilehash(f), f.getName()); System.out.println(url);//w w w. j a v a2 s. c o m HttpGet request = new HttpGet(url); CloseableHttpResponse r = httpclient.execute(request); System.out.println(r.getStatusLine()); HttpEntity e = r.getEntity(); String s = EntityUtils.toString(e); System.out.println(s); JSONArray json = JSONArray.fromObject(s); // JSONObject json = JSONObject.fromObject(s); System.out.println(json.size()); for (int i = 0; i < json.size(); i++) { System.out.println(i); JSONObject obj = json.getJSONObject(i); JSONArray fs = obj.getJSONArray("Files"); String downurl = fs.getJSONObject(0).getString("Link"); HttpGet r2 = new HttpGet(downurl); CloseableHttpResponse res2 = httpclient.execute(r2); // Header[] headers = res2.getAllHeaders(); // for(Header h:headers){ // System.out.println(h.getName()); // System.out.println(h.getValue()); // } Header header = res2.getFirstHeader("Content-Disposition"); String sig = "filename="; String v = header.getValue(); String fn = v.substring(v.indexOf(sig) + sig.length()); HttpEntity e2 = res2.getEntity(); File outf = new File(fn); FileOutputStream fos = new FileOutputStream(outf); e2.writeTo(fos); System.out.println(filecharsetdetect.FileCharsetDetect.detect(outf)); // res2.getEntity().writeTo(new FileOutputStream(fn)); System.out.println(fn); res2.close(); } r.close(); httpclient.close(); }
From source file:cn.digirun.frame.payment.wxpay.util.ClientCustomSSL.java
public static String doRefund(String url, String data) throws Exception { /**// w w w . java2s. co m * ?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: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. ja v a 2s .c o m*/ 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:org.ebaysf.ostara.upgrade.util.GitUtils.java
private static StringBuilder readData(Set<String> committers, String url) throws IOException, ClientProtocolException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response1 = httpclient.execute(httpGet); BufferedReader br = new BufferedReader(new InputStreamReader((response1.getEntity().getContent()))); if (response1.getStatusLine().getStatusCode() != 200) { LOG.warn("Unable to get git committers for " + url + " status code is " + response1.getStatusLine().getStatusCode()); return null; }//from w w w . ja v a 2 s. c o m StringBuilder sb = new StringBuilder(); String output; while ((output = br.readLine()) != null) { sb.append(output); } return sb; }
From source file:io.crate.rest.AdminUIIntegrationTest.java
private static void assertIsIndexResponse(CloseableHttpResponse response) throws IOException { //response body should not be null String bodyAsString = EntityUtils.toString(response.getEntity()); assertThat(bodyAsString, is("<h1>Crate Admin</h1>\n")); assertThat(response.getHeaders("Content-Type")[0].getValue(), is("text/html")); }
From source file:com.continuuity.loom.TestHelper.java
public static SchedulableTask takeTask(String loomUrl, TakeTaskRequest request) throws Exception { HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/take", loomUrl)); httpPost.setEntity(new StringEntity(GSON.toJson(request))); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); try {//from ww w. j a va 2 s .c om Assert.assertEquals(2, response.getStatusLine().getStatusCode() / 100); if (response.getEntity() == null) { return null; } return GSON.fromJson(EntityUtils.toString(response.getEntity()), SchedulableTask.class); } finally { response.close(); } }
From source file:com.portfolio.data.utils.PostForm.java
public static boolean updateResource(String sessionid, String backend, String uuid, String lang, String json) throws Exception { /// Parse and create xml from JSON JSONObject files = (JSONObject) JSONValue.parse(json); JSONArray array = (JSONArray) files.get("files"); if ("".equals(lang) || lang == null) lang = "fr"; JSONObject obj = (JSONObject) array.get(0); String ressource = ""; String attLang = " lang=\"" + lang + "\""; ressource += "<asmResource>" + "<filename" + attLang + ">" + obj.get("name") + "</filename>" + // filename "<size" + attLang + ">" + obj.get("size") + "</size>" + "<type" + attLang + ">" + obj.get("type") + "</type>" + // obj.get("url"); // Backend source, when there is multiple backend "<fileid" + attLang + ">" + obj.get("fileid") + "</fileid>" + "</asmResource>"; /// Send data to resource /// Server + "/resources/resource/file/" + uuid +"?lang="+ lang CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w ww. ja v a2 s . c o m HttpPut put = new HttpPut("http://" + backend + "/rest/api/resources/resource/" + uuid); put.setHeader("Cookie", "JSESSIONID=" + sessionid); // So that the receiving servlet allow us StringEntity se = new StringEntity(ressource); se.setContentEncoding("application/xml"); put.setEntity(se); CloseableHttpResponse response = httpclient.execute(put); try { HttpEntity resEntity = response.getEntity(); } finally { response.close(); } } finally { httpclient.close(); } return false; }
From source file:com.precioustech.fxtrading.oanda.restapi.OandaTestUtils.java
public static final void mockHttpInteraction(String fname, HttpClient mockHttpClient) throws Exception { CloseableHttpResponse mockResp = mock(CloseableHttpResponse.class); when(mockHttpClient.execute(any(HttpUriRequest.class))).thenReturn(mockResp); HttpEntity mockEntity = mock(HttpEntity.class); when(mockResp.getEntity()).thenReturn(mockEntity); StatusLine mockStatusLine = mock(StatusLine.class); when(mockResp.getStatusLine()).thenReturn(mockStatusLine); when(mockStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); when(mockEntity.getContent()).thenReturn(new FileInputStream(fname)); }