List of usage examples for org.apache.http.client.methods CloseableHttpResponse getEntity
HttpEntity getEntity();
From source file:com.thoughtworks.go.agent.service.TokenRequester.java
private String responseBody(CloseableHttpResponse response) throws IOException { try (InputStream is = response.getEntity() == null ? new NullInputStream(0) : response.getEntity().getContent()) { return IOUtils.toString(is, StandardCharsets.UTF_8); }// w w w .j ava2s. c o m }
From source file:net.oebs.jalos.Client.java
public String get(long id) throws IOException { HttpGet httpGet = new HttpGet(this.serviceUrl + "/a/" + id); CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String ret = response.getLastHeader("Location").toString(); EntityUtils.consume(entity);//from w w w . ja va 2 s . com response.close(); return ret; }
From source file:ch.ralscha.extdirectspring_itest.InfoServiceTest.java
@SuppressWarnings("unchecked") private static void testInfoPost(String method) throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); CloseableHttpResponse response = null; try {//from w w w .j a v a2 s .co m HttpPost post = new HttpPost("http://localhost:9998/controller/router"); List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("extTID", "1")); formparams.add(new BasicNameValuePair("extAction", "infoService")); formparams.add(new BasicNameValuePair("extMethod", method)); formparams.add(new BasicNameValuePair("extType", "rpc")); formparams.add(new BasicNameValuePair("extUpload", "false")); formparams.add(new BasicNameValuePair("userName", "RALPH")); UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); post.setEntity(postEntity); response = client.execute(post); HttpEntity entity = response.getEntity(); assertThat(entity).isNotNull(); String responseString = EntityUtils.toString(entity); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class); assertThat(rootAsMap).hasSize(5); assertThat(rootAsMap.get("method")).isEqualTo(method); assertThat(rootAsMap.get("type")).isEqualTo("rpc"); assertThat(rootAsMap.get("action")).isEqualTo("infoService"); assertThat(rootAsMap.get("tid")).isEqualTo(1); Map<String, Object> result = (Map<String, Object>) rootAsMap.get("result"); assertThat(result).hasSize(2); assertThat(result.get("user-name-lower-case")).isEqualTo("ralph"); assertThat(result.get("success")).isEqualTo(Boolean.TRUE); } finally { IOUtils.closeQuietly(response); IOUtils.closeQuietly(client); } }
From source file:io.djigger.monitoring.java.instrumentation.subscription.HttpClientTracerTest.java
private String parseResponse(CloseableHttpResponse response) throws Exception { String responseString = EntityUtils.toString(response.getEntity()); return responseString; }
From source file:net.oebs.jalos.Client.java
public SubmitResponseObject submit(String postUrl) throws IOException { HttpPost httpPost = new HttpPost(this.serviceUrl + "/a/submit"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("url", postUrl)); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); ObjectMapper mapper = new ObjectMapper(); SubmitResponseObject sro = mapper.readValue(entity.getContent(), SubmitResponseObject.class); EntityUtils.consume(entity);//ww w . j a v a 2 s.co m response.close(); return sro; }
From source file:org.wltea.analyzer.dic.Dictionary.java
/** * ???/*from w ww . ja v a 2 s . c om*/ */ private static List<String> getRemoteWords(String location) { List<String> buffer = new ArrayList<String>(); RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000) .setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000).build(); CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response; BufferedReader in; HttpGet get = new HttpGet(location); get.setConfig(rc); try { response = httpclient.execute(get); if (response.getStatusLine().getStatusCode() == 200) { String charset = "UTF-8"; // ??utf-8 if (response.getEntity().getContentType().getValue().contains("charset=")) { String contentType = response.getEntity().getContentType().getValue(); charset = contentType.substring(contentType.lastIndexOf("=") + 1); } in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset)); String line; while ((line = in.readLine()) != null) { buffer.add(line); } in.close(); response.close(); return buffer; } response.close(); } catch (ClientProtocolException e) { logger.error("getRemoteWords {} error", e, location); } catch (IllegalStateException e) { logger.error("getRemoteWords {} error", e, location); } catch (IOException e) { logger.error("getRemoteWords {} error", e, location); } return buffer; }
From source file:com.microsoft.azure.hdinsight.common.task.YarnHistoryTask.java
@Override public String call() throws Exception { CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider) .build();/*from www . ja v a2s .c o m*/ HttpGet httpGet = new HttpGet(path); httpGet.addHeader("Content-Type", "text/html"); CloseableHttpResponse response = httpclient.execute(httpGet); HttpEntity httpEntity = response.getEntity(); return IOUtils.toString(httpEntity.getContent()); }
From source file:gertjvr.slacknotifier.SlackApiProcessor.java
public void sendNotification(String url, SlackNotificationMessage notification) throws IOException { String content = new Gson().toJson(notification); logger.debug(String.format("sendNotification: %s", content)); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON)); CloseableHttpResponse response = httpClient.execute(httpPost); try {/*from w w w. j a v a 2 s . c om*/ HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } catch (Exception ex) { logger.error(String.format("sendNotification %s", ex.getMessage()), ex); } finally { response.close(); } }
From source file:org.geosdi.geoplatform.gui.GetMapUrlTest.java
@Test public void getMapUrlTest() throws Exception { CloseableHttpClient client = HttpClients.custom().build(); String url = "http://150.145.141.92/geoserver/wms?service=WMS&version=1.1.0&request=GetMap&layers=topp:states&styles=&bbox=1724753.45766073,5099790.02370763,1725687.17361712,5100561.22571727&width=512&height=422&srs=EPSG:3003&format=image/png"; HttpGet get = new HttpGet(url.replaceAll("&", "&")); CloseableHttpResponse response = client.execute(get); logger.info("###############################{}", response.getEntity().getContentType().getValue()); }
From source file:org.apache.clerezza.commons.rdf.impl.sparql.SparqlClient.java
public Object queryResult(final String query) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(endpoint); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("query", query)); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response2 = httpclient.execute(httpPost); HttpEntity entity2 = response2.getEntity(); try {//w ww . ja va 2 s. com InputStream in = entity2.getContent(); final String mediaType = entity2.getContentType().getValue(); if ("application/sparql-results+xml".equals(mediaType)) { return SparqlResultParser.parse(in); } else { //assuming RDF response //FIXME clerezza-core-rdf to clerezza dependency Parser parser = Parser.getInstance(); return parser.parse(in, mediaType); } } finally { EntityUtils.consume(entity2); response2.close(); } }