List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity) throws IOException, ParseException
From source file:com.zhongsou.souyue.service.SelfCreateUploadHttp.java
public static HttpJsonResponse doPost(String url, Map<String, String> params) { if (!CMainHttp.getInstance().isNetworkAvailable(MainApplication.getInstance())) return null; try {//from www. j av a 2 s.c o m HttpPost post = new HttpPost(url); // List<NameValuePair> pairs = new ArrayList<NameValuePair>(); // String value = null; // // for (Map.Entry<String, String> e : params.entrySet()) { // value = e.getValue(); // if (value != null) { // pairs.add(new BasicNameValuePair(e.getKey(), value.toString())); // } // } post.addHeader("User-Agent", AGENT); List<BasicNameValuePair> pairs = Utils.encryptToPostEntity(url, params, null); post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8)); HttpResponse resp = new DefaultHttpClient().execute(post); if (resp.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(resp.getEntity()); HttpJsonResponse json = new HttpJsonResponse((JsonObject) new JsonParser().parse(result)); return json; } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:net.officefloor.tutorial.testhttpserver.TemplateLogicIT.java
@Test public void integrationTest() throws Exception { try (CloseableHttpClient client = HttpTestUtil.createHttpClient()) { // Send request to add HttpGet request = new HttpGet("http://localhost:7878/template-add.woof?a=1&b=2"); HttpResponse response = client.execute(request); // Ensure added the values String entity = EntityUtils.toString(response.getEntity()); assertTrue("Should have added the values", entity.contains("= 3")); }//from w w w . j a v a2 s . c om }
From source file:org.commonjava.indy.client.core.IndyResponseErrorDetails.java
public IndyResponseErrorDetails(final HttpResponse response) { final StatusLine sl = response.getStatusLine(); this.code = sl.getStatusCode(); this.reason = sl.getReasonPhrase(); this.version = sl.getProtocolVersion(); String body = null;/*from ww w . j av a 2 s . co m*/ if (response.getEntity() != null) { try { body = EntityUtils.toString(response.getEntity()); } catch (final ParseException e) { logger.debug("Failed to retrieve error response body.", e); } catch (final IOException e) { logger.debug("Failed to retrieve error response body.", e); } } this.body = body; }
From source file:org.urbantower.j4s.example.springmvc.SpringMvcTest.java
@Test public void isJettyServerRunning() throws InterruptedException, IOException { CloseableHttpResponse response = httpclient.execute(new HttpGet("http://localhost:9091/webapp/helloworld")); String body = EntityUtils.toString(response.getEntity()); Assert.assertTrue(body.contains("Hello World!")); }
From source file:org.kitodo.data.elasticsearch.index.type.DocketTypeTest.java
@Test public void shouldCreateDocument() throws Exception { DocketType docketType = new DocketType(); Docket docket = prepareData().get(0); HttpEntity document = docketType.createDocument(docket); JSONParser parser = new JSONParser(); JSONObject actual = (JSONObject) parser.parse(EntityUtils.toString(document)); JSONObject excepted = (JSONObject) parser.parse("{\"title\":\"default\",\"file\":\"docket.xsl\"}"); assertEquals("Docket JSONObject doesn't match to given JSONObject!", excepted, actual); }
From source file:org.nuxeo.elasticsearch.http.readonly.HttpClient.java
public static String get(String url) throws IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); try (CloseableHttpResponse response = client.execute(httpget)) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; }// www . j a v a 2 s.com }
From source file:com.microsoft.azure.hdinsight.sdk.rest.ObjectConvertUtils.java
public static <T> Optional<T> convertEntityToObject(@NotNull HttpEntity entity, @NotNull Class<T> tClass) throws IOException { final String type = entity.getContentType().getValue().toLowerCase(); switch (type) { case "application/json": return convertJsonToObject(EntityUtils.toString(entity), tClass); case "application/xml": return convertXmlToObject(EntityUtils.toString(entity), tClass); }/*from w w w . ja v a 2s . com*/ return Optional.empty(); }
From source file:org.kitodo.data.index.elasticsearch.type.DocketTypeTest.java
@Test public void shouldCreateDocument() throws Exception { DocketType docketType = new DocketType(); Docket docket = prepareData().get(0); HttpEntity document = docketType.createDocument(docket); JSONParser parser = new JSONParser(); JSONObject docketObject = (JSONObject) parser.parse(EntityUtils.toString(document)); String actual = String.valueOf(docketObject.get("name")); String excepted = "default"; assertEquals("Docket value for name key doesn't match to given plain text!", excepted, actual); actual = String.valueOf(docketObject.get("file")); excepted = "docket.xsl"; assertEquals("Docket value for file key doesn't match to given plain text!", excepted, actual); }
From source file:pingdesktop.Sample.HTTPhandlerSample.java
private void doGet() throws IOException { /* create the HTTP client and GET request */ HttpGet httpGet = new HttpGet("http://hc.apache.org/"); /* execute request */ HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); /* process response */ if (httpResponse.getStatusLine().getStatusCode() == 200) { String responseText = EntityUtils.toString(httpEntity); System.out.println(responseText); } else {/*from w ww . j a va 2 s .c o m*/ System.err.println("Invalid HTTP response: " + httpResponse.getStatusLine().getStatusCode()); } }
From source file:org.blanco.techmun.android.misc.XmlParser.java
public static synchronized JSONArray parseJSONArrayFromHttpEntity(HttpEntity entity) throws Exception { String jsonText = null;/* w ww .j av a 2 s . co m*/ try { jsonText = EntityUtils.toString(entity); JSONArray result = new JSONArray(jsonText); return result; } catch (ParseException e) { throw new Exception("Error parsing the entity.", e); } catch (IOException e) { throw new Exception("Error reading the entity.", e); } }