List of usage examples for org.apache.http.util EntityUtils toString
public static String toString(HttpEntity httpEntity) throws IOException, ParseException
From source file:com.elastic.support.diagnostics.RestModule.java
public String submitRequest(String url) { HttpResponse response = null;//from w w w.j av a 2s . co m String result = ""; try { HttpGet httpget = new HttpGet(url); response = client.execute(httpget); try { org.apache.http.HttpEntity entity = response.getEntity(); if (entity != null) { checkResponseCode(url, response); result = EntityUtils.toString(entity); } } catch (Exception e) { logger.error("Error handling response for " + url, e); } finally { HttpClientUtils.closeQuietly(response); } } catch (HttpHostConnectException e) { throw new RuntimeException("Error connecting to host " + url, e); } catch (Exception e) { if (e.getMessage().contains("401 Unauthorized")) { logger.error("Auth failure", e); throw new RuntimeException("Authentication failure: invalid login credentials.", e); } else { logger.error("Diagnostic query: " + url + "failed.", e); } } return result; }
From source file:com.roncoo.pay.permission.utils.RoncooHttpClientUtils.java
/** * API//from w w w . ja v a 2 s. c o m * * @param parameters * @return */ @SuppressWarnings({ "resource", "deprecation" }) public static String post(String url, String parameters) { HttpClient httpClient = new DefaultHttpClient(); HttpPost method = new HttpPost(url); String body = null; if (method != null & parameters != null && !"".equals(parameters.trim())) { try { // NameValuePair?? method.addHeader("Content-type", "application/json; charset=utf-8"); method.setHeader("Accept", "application/json"); method.setEntity(new StringEntity(parameters, Charset.forName("UTF-8"))); HttpResponse response = httpClient.execute(method); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return "1";// 1 } // Read the response body body = EntityUtils.toString(response.getEntity()); } catch (IOException e) { // return "2"; } finally { } } return body; }
From source file:cn.org.once.cstack.utils.TestUtils.java
/** * Return the content of an URL.//w w w . j a v a2 s. com * * @param url * @return * @throws ParseException * @throws IOException */ public static String getUrlContentPage(String url) throws ParseException, IOException { HttpGet request = new HttpGet(url); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity); }
From source file:net.oneandone.shared.artifactory.JsonResponseHandler.java
@Override public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException { final HttpEntity entity = returnEntityWhenStatusValid(response); final String body = EntityUtils.toString(entity); return Utils.createGson().fromJson(body, clazz); }
From source file:org.sharetask.data.IntegrationTest.java
@BeforeClass public static void login() throws Exception { final DefaultHttpClient client = new DefaultHttpClient(); final HttpPost httpPost = new HttpPost(BASE_URL + "/user/login"); httpPost.addHeader(new BasicHeader("Content-Type", "application/json")); final StringEntity httpEntity = new StringEntity( "{\"username\":\"dev1@shareta.sk\"," + "\"password\":\"password\"}"); System.out.println(EntityUtils.toString(httpEntity)); httpPost.setEntity(httpEntity);/* w ww . ja v a 2 s . co m*/ //when final HttpResponse response = client.execute(httpPost); //then Assert.assertEquals(HttpStatus.OK.value(), response.getStatusLine().getStatusCode()); client.getCookieStore().getCookies(); for (final Cookie cookie : client.getCookieStore().getCookies()) { if (cookie.getName().equals("JSESSIONID")) { DOMAIN = cookie.getDomain(); SESSIONID = cookie.getValue(); } } }
From source file:com.google.appengine.tck.misc.http.support.Client.java
public String post(String url) throws Exception { HttpPost post = new HttpPost(url); HttpResponse response = getClient().execute(post); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); return EntityUtils.toString(response.getEntity()); }
From source file:sendsms.WebService.java
public String get(String url) { String source = null;/* w w w. jav a2 s . c o m*/ HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); try { HttpResponse httpResponse = httpClient.execute(httpGet); source = EntityUtils.toString(httpResponse.getEntity()); } catch (IOException e) { e.printStackTrace(); } return source; }
From source file:com.flowzr.http.HttpClientWrapper.java
public String getAsString(String url) throws Exception { HttpGet get = new HttpGet(url); HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects", false); get.setParams(params);/* www .j a v a 2s . c o m*/ HttpResponse r = httpClient.execute(get); return EntityUtils.toString(r.getEntity()); }
From source file:com.android.dialer.lookup.LookupUtils.java
public static String httpGet(HttpGet request) throws IOException { HttpClient client = new DefaultHttpClient(); request.setHeader("User-Agent", USER_AGENT); HttpResponse response = client.execute(request); int status = response.getStatusLine().getStatusCode(); // Android's org.apache.http doesn't have the RedirectStrategy class if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) { Header[] headers = response.getHeaders("Location"); if (headers != null && headers.length != 0) { HttpGet newGet = new HttpGet(headers[headers.length - 1].getValue()); for (Header header : request.getAllHeaders()) { newGet.addHeader(header); }//w w w . j a v a2 s . co m return httpGet(newGet); } else { throw new IOException("Empty redirection header"); } } if (status != HttpStatus.SC_OK) { throw new IOException("HTTP failure (status " + status + ")"); } return EntityUtils.toString(response.getEntity()); }
From source file:com.continuuity.loom.TestHelper.java
public static SchedulableTask takeTask(String loomUrl, TakeTaskRequest request) throws Exception { HttpPost httpPost = new HttpPost(String.format("%s/v1/loom/tasks/take", loomUrl)); httpPost.setEntity(new StringEntity(GSON.toJson(request))); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); try {//w w w . j av a 2 s .c o m Assert.assertEquals(2, response.getStatusLine().getStatusCode() / 100); if (response.getEntity() == null) { return null; } return GSON.fromJson(EntityUtils.toString(response.getEntity()), SchedulableTask.class); } finally { response.close(); } }