List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity) throws IOException, ParseException
From source file:ph.sakay.gateway.RoutingService.java
private String request(String sender, String message) { try {//from www . j a v a 2s. c o m Log.d("RoutingService", "Querying server"); HttpResponse response = new APIClient(this).get("/sms", "?target=" + Uri.encode(getOwnNumber()) + "&body=" + Uri.encode(message) + "&source=" + Uri.encode(sender)); int status = response.getStatusLine().getStatusCode(); String body = EntityUtils.toString(response.getEntity()); if (status >= 200 && status < 300) { Log.d("RoutingService", "Got server response: " + body); return body; } else { Log.e("RoutingService", "Error while querying server. Got status: " + status + " and response: " + body); return "Sorry, there is a problem with the server."; } } catch (Exception e) { Log.d("RoutingService", "Error while querying server.", e); return "Sorry, an error has occurred."; } }
From source file:com.aspire.mandou.framework.http.BetterHttpResponseImpl.java
@Override public String getResponseBodyAsString() throws IOException { return EntityUtils.toString(entity); }
From source file:com.ibm.watson.developer_cloud.professor_languo.it.endpoints.IT_GetThread.java
/** * Test what happens when a properly formated id request is sent to endpoint * //from w w w .j a v a 2s .c o m * @throws Exception */ @Test public void getThreadWithInteger() throws Exception { // Set up the connection String id = String.valueOf(Integer.MAX_VALUE); String getUrl = GET_URL + id; // Execute the query and get the response HttpResponse response = httpClient.execute(new HttpGet(getUrl)); String jsonString = EntityUtils.toString(response.getEntity()); // The response has to be a 200 if the id is found or 404 if it is not Assert.assertTrue("response was: " + jsonString, response.getStatusLine().getStatusCode() == 404 || response.getStatusLine().getStatusCode() == 200); }
From source file:ar.com.colo.rest.test.RestletTestSupport.java
public static void assertHttpResponse(HttpResponse response, int expectedStatusCode, String expectedContentType, String expectedBody) throws ParseException, IOException { assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode()); assertTrue(response.getFirstHeader("Content-Type").getValue().startsWith(expectedContentType)); if (expectedBody != null) { assertEquals(expectedBody, EntityUtils.toString(response.getEntity())); }//from w ww .ja v a2 s. c o m }
From source file:org.urbantower.j4s.example.autowired.AutowiredHandlersTest.java
@Test public void isJettyServerRunning() throws InterruptedException, IOException { HttpGet request = new HttpGet("http://localhost:9093/test/servlet"); CloseableHttpResponse response = httpclient.execute(request); String body = EntityUtils.toString(response.getEntity()); Assert.assertEquals(body, "Hello Servlet"); }
From source file:com.lixiaocong.util.weather.WeatherHelper.java
public static Weather getWeather(String city) throws IOException { HttpClient httpClient = HttpClients.custom().build(); HttpGet httpGet = new HttpGet("http://apis.baidu.com/apistore/weatherservice/weather?citypinyin=" + city); httpGet.addHeader("apikey", "f6e1b7c306d087df20761632655bc18b"); RequestConfig config = RequestConfig.custom().setConnectTimeout(500).build(); httpGet.setConfig(config);//w w w . jav a 2s. co m HttpResponse response = httpClient.execute(httpGet); String jsonString = EntityUtils.toString(response.getEntity()); logger.info("weather server returns " + jsonString); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(jsonString, Weather.class); }
From source file:org.elasticsearch.packaging.util.ServerUtils.java
public static void waitForElasticsearch(String status, String index) throws IOException { Objects.requireNonNull(status); // we loop here rather than letting httpclient handle retries so we can measure the entire waiting time final long startTime = System.currentTimeMillis(); long timeElapsed = 0; boolean started = false; while (started == false && timeElapsed < waitTime) { try {/*www . j a va 2 s. co m*/ final HttpResponse response = Request.Get("http://localhost:9200/_cluster/health") .connectTimeout((int) timeoutLength).socketTimeout((int) timeoutLength).execute() .returnResponse(); if (response.getStatusLine().getStatusCode() >= 300) { final String statusLine = response.getStatusLine().toString(); final String body = EntityUtils.toString(response.getEntity()); throw new RuntimeException( "Connecting to elasticsearch cluster health API failed:\n" + statusLine + "\n" + body); } started = true; } catch (HttpHostConnectException e) { // we want to retry if the connection is refused LOG.debug("Got connection refused when waiting for cluster health", e); } timeElapsed = System.currentTimeMillis() - startTime; } if (started == false) { throw new RuntimeException("Elasticsearch did not start"); } final String url; if (index == null) { url = "http://localhost:9200/_cluster/health?wait_for_status=" + status + "&timeout=60s&pretty"; } else { url = "http://localhost:9200/_cluster/health/" + index + "?wait_for_status=" + status + "&timeout=60s&pretty"; } final String body = makeRequest(Request.Get(url)); assertThat("cluster health response must contain desired status", body, containsString(status)); }
From source file:org.kitodo.data.elasticsearch.index.type.RulesetTypeTest.java
@Test public void shouldCreateDocument() throws Exception { RulesetType rulesetType = new RulesetType(); Ruleset ruleset = prepareData().get(0); JSONParser parser = new JSONParser(); HttpEntity document = rulesetType.createDocument(ruleset); JSONObject actual = (JSONObject) parser.parse(EntityUtils.toString(document)); JSONObject excepted = (JSONObject) parser.parse( "{\"title\":\"SLUBDD\",\"file\":\"ruleset_slubdd.xml\",\"orderMetadataByRuleset\":false\"fileContent\":\"\"}"); assertEquals("Ruleset JSONObject doesn't match to given JSONObject!", excepted, actual); }
From source file:com.predic8.membrane.servlet.test.ReleaseConfigurationTest.java
@Test public void testReachable() throws ClientProtocolException, IOException { String secret = "Web Services"; HttpClient hc = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(getBaseURL()); post.setEntity(new StringEntity(secret)); HttpResponse res = hc.execute(post); assertEquals(200, res.getStatusLine().getStatusCode()); AssertUtils.assertContains(secret, EntityUtils.toString(res.getEntity())); }
From source file:com.flowzr.http.HttpClientWrapper.java
public String getAsStringIfOk(String url) throws Exception { HttpGet get = new HttpGet(url); HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects", false); get.setParams(params);//from w ww. j a va 2 s . c o m HttpResponse r = httpClient.execute(get); String s = EntityUtils.toString(r.getEntity()); if (r.getStatusLine().getStatusCode() == 200) { return s; } else { throw new RuntimeException(s); } }