List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity) throws IOException, ParseException
From source file:org.esigate.test.conn.SequenceResponseTest.java
@Test public void testSequence() throws IOException { SequenceResponse seq = new SequenceResponse() .response(new HttpResponseBuilder().status(HttpStatus.SC_OK).entity("OK 1").build()) .response(new HttpResponseBuilder().status(HttpStatus.SC_OK).entity("OK 2").build()); assertEquals("OK 1", EntityUtils.toString(seq.execute(IncomingRequest.builder("http://locahost").build()).getEntity())); assertEquals("OK 2", EntityUtils.toString(seq.execute(IncomingRequest.builder("http://locahost").build()).getEntity())); try {/*from w ww . j a v a 2s.co m*/ seq.execute(IncomingRequest.builder("http://locahost").build()); fail("Should send an exception"); } catch (IllegalStateException e) { // OK } }
From source file:fm.last.musicbrainz.coverart.impl.FetchJsonListingResponseHandler.java
@Override public String handleResponse(HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { return EntityUtils.toString(entity); } else if (statusLine.getStatusCode() == HttpStatus.SC_NOT_FOUND) { return null; }/*from ww w .ja v a 2s. c o m*/ HttpUtil.consumeEntity(entity); throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); }
From source file:com.meltmedia.cadmium.blackbox.test.AbstractBodyApiResponseValidator.java
@Override public void validate(HttpResponse response) { try {/*from w w w .j a va 2 s . c o m*/ super.validate(response); } catch (AssertionError e) { String responseBody = null; try { responseBody = EntityUtils.toString(response.getEntity()); } catch (Exception e1) { fail(e.getMessage() + ": Failed to dump response body."); } fail(e.getMessage() + ": " + responseBody); } try { validateBody(response, EntityUtils.toString(response.getEntity())); } catch (Exception e) { fail("Failed to validate body: " + e.getMessage()); } }
From source file:org.house.service.spider.WebSpider.java
private static synchronized String doHttpRequest(final HttpRequestBase httpRequestBase) { if (lastCallTime == -1) { lastCallTime = System.currentTimeMillis(); }/*w w w .jav a 2s. c o m*/ while (true) { if (System.currentTimeMillis() - lastCallTime < 300) { try { Thread.sleep(200); } catch (final InterruptedException e) { e.printStackTrace(); } } else { lastCallTime = System.currentTimeMillis(); break; } } try { final CloseableHttpResponse theResponse = client.execute(httpRequestBase); return EntityUtils.toString(theResponse.getEntity()); } catch (final Exception e) { e.printStackTrace(); return null; } }
From source file:xworker.app.model.tree.http.HttpExtjsJsonTreeModelActions.java
/** * ????/*from www . j a v a 2 s . c o m*/ * * @param url * @param self * @param actionContext * @return * @throws IOException */ @SuppressWarnings("unchecked") public static Object getTreeDatas(String prefixUrl, String url, Thing self, ActionContext actionContext) throws IOException { HttpClient httpClient = getHttpClient(self, actionContext); if (httpClient != null) { HttpGet httpGet = new HttpGet(url); HttpEntity entity = null; try { HttpResponse response = httpClient.execute(httpGet); entity = response.getEntity(); String content = EntityUtils.toString(entity); Object datas = JsonFormator.parse(content, actionContext); URI uri = null; if (prefixUrl != null && !"".equals(prefixUrl)) { //prefixUrl uri = new URI(prefixUrl); } else { uri = new URI(url); } if (datas instanceof List) { checkImageUrl((List<Map<String, Object>>) datas, uri); } else { checkImageUrl((Map<String, Object>) datas, uri); } return datas; } catch (Exception e) { logger.error("Get tree content error", e); } finally { if (entity != null) { EntityUtils.consume(entity); } } } return null; }
From source file:com.foobnix.util.LyricUtil.java
public static String fetchLyric(String artist, String title) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("artist", artist)); params.add(new BasicNameValuePair("song", title)); params.add(new BasicNameValuePair("fmt", "json")); String paramsList = URLEncodedUtils.format(params, "UTF-8"); HttpGet request = new HttpGet("http://lyricwiki.org/api.php?" + paramsList); HttpClient client = new DefaultHttpClient(); try {//w w w.j a v a 2s .co m HttpResponse execute = client.execute(request); String response = EntityUtils.toString(execute.getEntity()); LOG.d(response); if (response != null && response.indexOf('{') > 0) { JSONObject obj = new JSONObject(response.substring(response.indexOf('{'))); return obj.getString("lyrics"); } } catch (ClientProtocolException e) { LOG.e("", e); } catch (IOException e) { LOG.e("", e); } catch (JSONException e) { LOG.e("", e); } return "Lyric not found"; }
From source file:com.kingmed.dp.ndp.impl.NDPServeResponseHandler.java
@Override public String handleResponse(HttpResponse hr) throws ClientProtocolException, IOException { String responseBody = null;//from w w w . j a v a 2s . c o m int status = hr.getStatusLine().getStatusCode(); HttpEntity entity = hr.getEntity(); responseBody = (entity != null ? EntityUtils.toString(entity) : null); log.info("?" + status + "\r\n\r\n[" + responseBody + "]"); if (status >= 200 && status < 300) { Header[] cookies = hr.getHeaders("Set-Cookie"); for (Header c : cookies) { String v = c.getValue(); if (!v.endsWith("=")) { setCookie(v); log.info("cookie=[" + cookie + "]"); } } // HttpEntity entity = hr.getEntity(); // responseBody = (entity != null ? EntityUtils.toString(entity) : null); // log.info("\r\n" + responseBody); } return responseBody; }
From source file:no.hig.gsd.quizgame.ServerUtilities.java
/** * Register this account/device pair within the server. * * @return whether the registration succeeded or not. */// w w w . j av a2 s. c o m public static String register(final Context context, final String regId) { Log.i("remote", "registering device (regId = " + regId + ")"); String usm = LoginActivity.usm; String retSrc = ""; try { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://89.250.116.142/Quizgame/jaxrs/quizgame/gcm"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("username", usm)); nameValuePairs.add(new BasicNameValuePair("regId", regId)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); HttpResponse httpResponse = client.execute(post); HttpEntity entity = httpResponse.getEntity(); retSrc = EntityUtils.toString(entity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (retSrc.equals("success")) { return status = "success"; } else { return status = "failure"; } }
From source file:org.esigate.test.conn.UriResponseTest.java
@Test public void testUri() throws IOException { UriResponse seq = new UriResponse() .response("http://test/path1", new HttpResponseBuilder().status(HttpStatus.SC_OK).entity("OK 1").build()) .response("http://test/path2", new HttpResponseBuilder().status(HttpStatus.SC_OK).entity("OK 2").build()); assertEquals("OK 1", EntityUtils .toString(seq.execute(IncomingRequest.builder("http://test/path1").build()).getEntity())); assertEquals("OK 2", EntityUtils .toString(seq.execute(IncomingRequest.builder("http://test/path2").build()).getEntity())); try {/*from www .j a v a 2s. com*/ seq.execute(IncomingRequest.builder("http://test/path3").build()); fail("Should send an exception"); } catch (IllegalStateException e) { // OK } }
From source file:org.keycloak.testsuite.util.matchers.HttpResponseBodyMatcher.java
@Override public boolean matches(Object item) { try {/*from w w w . ja va 2s .com*/ return (item instanceof HttpResponse) && this.matcher.matches(EntityUtils.toString(((HttpResponse) item).getEntity())); } catch (IOException e) { throw new RuntimeException(e); } }