List of usage examples for org.apache.http.util EntityUtils consume
public static void consume(HttpEntity httpEntity) throws IOException
From source file:org.elasticsearch.river.solr.support.SolrIndexer.java
public void indexDocuments(Map<String, Map<String, Object>> documents) throws IOException { StringBuilder jsonBuilder = new StringBuilder("["); int i = 0;/*from ww w .j av a2s.c o m*/ for (Map<String, Object> doc : documents.values()) { jsonBuilder.append(objectMapper.writeValueAsString(doc)); if (i < documents.values().size() - 1) { jsonBuilder.append(","); } i++; } jsonBuilder.append("]"); HttpPost httpPost = new HttpPost(solrUrl + "?commit=true"); httpPost.setHeader("Content-type", "application/json"); httpPost.setEntity(new StringEntity(jsonBuilder.toString())); CloseableHttpResponse response = httpClient.execute(httpPost); try { EntityUtils.consume(response.getEntity()); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("documents were not properly indexed"); } } finally { EntityUtils.consume(response.getEntity()); response.close(); } }
From source file:myexamples.MyExamples.java
public static String getTweet() throws IOException { String result = ""; HttpGet httpGet = new HttpGet("http://localhost:9200/gb/tweet/9"); CloseableHttpResponse response1 = httpclient.execute(httpGet); // The underlying HTTP connection is still held by the response object // to allow the response content to be streamed directly from the network socket. // In order to ensure correct deallocation of system resources // the user MUST call CloseableHttpResponse#close() from a finally clause. // Please note that if response content is not fully consumed the underlying // connection cannot be safely re-used and will be shut down and discarded // by the connection manager. try {//from w w w . ja v a 2s.c o m System.out.println(response1.getStatusLine()); HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed if (entity1 != null) { long len = entity1.getContentLength(); if (len != -1 && len < 2048) { // System.out.println(EntityUtils.toString(entity1)); result = EntityUtils.toString(entity1); } else { System.out.println("entity length=" + len); } } EntityUtils.consume(entity1); } finally { response1.close(); } return result; }
From source file:org.jasig.cas.web.LicenceFilter.java
public void init(FilterConfig config) throws ServletException { //licence// ww w .j a va2s . co m String liurl = config.getInitParameter("liurl"); if (liurl == null || liurl.isEmpty()) { throw new ServletException(); } CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(liurl); httpGet.addHeader("accept", "application/json"); CloseableHttpResponse response1 = null; try { response1 = httpclient.execute(httpGet); if (response1.getStatusLine().getStatusCode() != 200) { System.out.println("licence "); throw new ServletException(); } HttpEntity entity1 = response1.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader((entity1.getContent()))); String output; StringBuilder sb = new StringBuilder(); while ((output = br.readLine()) != null) { sb.append(output); } JSONObject jsonObject = new JSONObject(sb.toString()); String error = jsonObject.getString("code"); if (!error.equals("S_OK")) { System.out.println("licence "); throw new ServletException(); } String strexprietime = jsonObject.getJSONObject("var").getJSONObject("licInfo").getString("expireTime"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); this.expiretime = df.parse(strexprietime).getTime(); EntityUtils.consume(entity1); } catch (Exception e) { e.printStackTrace(); } finally { try { response1.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.hy.utils.pay.wx.ClientCustomSSL.java
public static void test() throws Exception { KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("D:/10016225.p12")); try {/* w w w . ja v a2 s. c om*/ keyStore.load(instream, "10016225".toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "10016225".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 { HttpGet httpget = new HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund"); System.out.println("executing request" + httpget.getRequestLine()); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); 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; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } } EntityUtils.consume(entity); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:adapter.sos.BasicSensorObservationServiceClient.java
public void postXml(String strXML, String serviceURL) throws SOSException { HttpPost httppost = new HttpPost(serviceURL); HttpEntity entity = null;/* w ww . j a va 2 s .c o m*/ HttpResponse response = null; try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("request", strXML)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); response = httpclient.execute(httppost); entity = response.getEntity(); int code = response.getStatusLine().getStatusCode(); if (code != 200) { throw new SOSException("Server replied with http error code: " + code); } examineReponse(entity); } catch (IOException e) { try { EntityUtils.consume(entity); } catch (IOException e1) { e1.printStackTrace(); } throw new SOSException("Cannot execute post: " + e.getCause()); } }
From source file:org.callimachusproject.server.HttpResponseTest.java
public void testEcho() throws Exception { Echo echo = (Echo) con.getObject(client.path("/echo").toString()); HttpResponse resp = echo.echo("text/alpha", "abc"); assertEquals("text/alpha", resp.getEntity().getContentType().getValue()); ByteArrayOutputStream out = new ByteArrayOutputStream(); HttpEntity entity = resp.getEntity(); try {//w ww .j a v a 2 s . c o m entity.writeTo(out); } finally { EntityUtils.consume(entity); } assertEquals("abc", out.toString("UTF-8")); }
From source file:net.acesinc.data.json.generator.log.HttpPostLogger.java
private void logEvent(String event) { try {//from w ww .jav a 2s.co m HttpPost request = new HttpPost(url); StringEntity input = new StringEntity(event); input.setContentType("application/json"); request.setEntity(input); // log.debug("executing request " + request); CloseableHttpResponse response = null; try { response = httpClient.execute(request); } catch (IOException ex) { log.error("Error POSTing Event", ex); } if (response != null) { try { // log.debug("----------------------------------------"); // log.debug(response.getStatusLine().toString()); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { // log.debug("Response content length: " + resEntity.getContentLength()); } EntityUtils.consume(resEntity); } catch (IOException ioe) { //oh well } finally { try { response.close(); } catch (IOException ex) { } } } } catch (Exception e) { } }
From source file:org.wso2.developerstudio.appfactory.core.client.HttpsJaggeryClient.java
public static String httpPostLogin(String urlStr, Map<String, String> params) { client = new DefaultHttpClient(); client = HttpsJaggeryClient.wrapClient(client, urlStr); HttpPost post = new HttpPost(urlStr); String respond = ""; HttpResponse response = null;/* w w w.ja v a 2s . c om*/ try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); Set<String> keySet = params.keySet(); for (String key : keySet) { nameValuePairs.add(new BasicNameValuePair(key, params.get(key))); } post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); response = client.execute(post); if (200 == response.getStatusLine().getStatusCode()) { cookie = response.getFirstHeader("Set-Cookie").getValue().split(";")[0]; HttpEntity entityGetAppsOfUser = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entityGetAppsOfUser.getContent())); StringBuilder sb = new StringBuilder(); String line = ""; while ((line = rd.readLine()) != null) { sb.append(line); } respond = sb.toString(); if ("false".equals(respond)) { Authenticator.getInstance().setErrorcode(ErrorType.INVALID); } EntityUtils.consume(entityGetAppsOfUser); if (entityGetAppsOfUser != null) { entityGetAppsOfUser.getContent().close(); } } else { Authenticator.getInstance().setErrorcode(ErrorType.FAILD); Authenticator.getInstance().setErrormsg(response.getStatusLine().getReasonPhrase()); log.error("(" + response.getStatusLine().getStatusCode() + ")" + ":" + response.getStatusLine().getReasonPhrase()); return "false"; } } catch (Exception e) { Authenticator.getInstance().setErrorcode(ErrorType.ERROR); log.error("Connection failure", e); return "false"; } finally { client.getConnectionManager().closeExpiredConnections(); } return respond; }
From source file:com.sonatype.nexus.perftest.maven.DownloadAction.java
private String getUrlContents(String url) throws IOException { final HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); if (!isSuccess(response)) { EntityUtils.consume(response.getEntity()); return null; }// w w w. jav a 2 s . c om return EntityUtils.toString(response.getEntity(), (Charset) null); }
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 . j a v a 2s . co 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(); } }