Example usage for org.apache.http.util EntityUtils toString

List of usage examples for org.apache.http.util EntityUtils toString

Introduction

In this page you can find the example usage for org.apache.http.util EntityUtils toString.

Prototype

public static String toString(HttpEntity httpEntity) throws IOException, ParseException 

Source Link

Usage

From source file:fichiercentraltheses.PersonXMLCaller.java

public String callForXMLResponse() throws URISyntaxException, IOException {
    HttpClient httpclient = new DefaultHttpClient();

    URIBuilder builder = new URIBuilder();
    builder.setScheme("http").setHost("www.theses.fr").setPath("/personnes/").setParameter("q", "levallois")
            .setParameter("format", "xml");

    URI uri = builder.build();/* ww w . ja  v a 2s  .  c  o  m*/
    HttpGet httpget = new HttpGet(uri);
    //        HttpGet httpget = new HttpGet("http://ws.seloger.com/search.xml?ci=690381,690382,690383,690387&idqfix=1&idtt=2&idtypebien=1&tri=d_dt_crea");

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    if (entity == null) {
        System.out.println("empty response to API call");
    }
    String responseString = EntityUtils.toString(entity);
    System.out.println(responseString);
    return responseString;
}

From source file:ch.ralscha.extdirectspring_itest.ModelGeneratorControllerTest.java

@Test
public void testAuthor() throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet g = new HttpGet("http://localhost:9998/controller/Author.js");
    HttpResponse response = client.execute(g);
    String responseString = EntityUtils.toString(response.getEntity());
    String contentType = response.getFirstHeader("Content-Type").getValue();
    String etag = response.getFirstHeader("ETag").getValue();

    GeneratorTestUtil.compareExtJs4Code("Author", responseString, false, false);
    assertThat(contentType).isEqualTo("application/javascript;charset=UTF-8");
    assertThat(etag).isNotEmpty();/*  w w  w .  j  a va  2 s  . co  m*/

    g = new HttpGet("http://localhost:9998/controller/Author.js");
    g.addHeader("If-None-Match", etag);
    response = client.execute(g);
    assertThat(response.getStatusLine().getStatusCode()).isEqualTo(304);
}

From source file:com.meltmedia.cadmium.blackbox.test.BasicBodyApiResponseValidator.java

@Override
public void validate(HttpResponse response) {
    super.validate(response);
    try {/*  w  w  w.  j  a  v a2  s  .c o  m*/
        String content = EntityUtils.toString(response.getEntity());
        assertEquals("Response Body {" + content + "} does not match the expected body content: "
                + this.expectedBodyContent, content, this.expectedBodyContent);
    } catch (IOException e) {
        fail("Failed with exception: " + e.getMessage());
    }
}

From source file:com.gozap.chouti.service.HttpService.java

/**
 *
 * @param uri uri/*  w ww.java  2  s  . c o m*/
 * @return
 * @author saint
 * @date 2013-4-17
 */
public static Boolean post(String uri, UrlEncodedFormEntity entity) {
    HttpClient httpClient = HttpClientFactory.getInstance();
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(entity);
    try {
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        String jsonStr = EntityUtils.toString(httpEntity);
        JSONObject json = JSON.parseObject(jsonStr);
        if (!json.getString("code").equals("99999")) {
            LOGGER.error("post uri:" + uri + ":" + entity + "error!" + "error msg: " + jsonStr);
        } else {
            return true;
        }
    } catch (ClientProtocolException e) {
        LOGGER.error("get" + uri + "error", e);
    } catch (IOException e) {
        LOGGER.error("get" + uri + "error", e);
    } finally {
        httpPost.releaseConnection();
    }

    return false;
}

From source file:com.yozio.android.FakeHttpClient.java

public Uri getLastRequestUri() {
    HttpEntity entity = lastRequest.getEntity();
    String requestParams;//from  w w w.  ja v a2  s.  c om
    try {
        requestParams = DEFAULT_BASE_URL + "?" + EntityUtils.toString(entity);
        return Uri.parse(requestParams);
    } catch (IOException e) {
    }
    return null;
}

From source file:org.fcrepo.integration.api.FedoraObjectsIT.java

@Test
public void testIngest() throws Exception {
    final HttpPost method = postObjMethod("FedoraObjectsTest1");
    final HttpResponse response = client.execute(method);
    assertEquals(201, response.getStatusLine().getStatusCode());
    final String content = EntityUtils.toString(response.getEntity());
    assertTrue("Response wasn't a PID", compile("[a-z]+").matcher(content).find());
    final String location = response.getFirstHeader("Location").getValue();
    assertEquals("Got wrong Location header for ingest!",
            serverAddress + OBJECT_PATH.replace("/", "") + "/FedoraObjectsTest1", location);
}

From source file:org.debux.webmotion.server.tools.StringResponseHandler.java

@Override
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        return EntityUtils.toString(entity);
    } else {// w  w  w .  ja  v  a 2 s. c o  m
        return null;
    }
}

From source file:co.cask.cdap.gateway.handlers.PingHandlerTestRun.java

@Test
public void testStatus() throws Exception {
    HttpResponse response = GatewayFastTestsSuite.doGet("/status");
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    Assert.assertEquals(Constants.Monitor.STATUS_OK, EntityUtils.toString(response.getEntity()));
}

From source file:org.drftpd.util.HttpUtils.java

public static String retrieveHttpAsString(String url) throws HttpException, IOException {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000)
            .setConnectionRequestTimeout(5000).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
            .setUserAgent(_userAgent).build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);// w w w . java2s  .  c  o  m
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpException("Error " + statusCode + " for URL " + url);
        }
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new IOException("Error for URL " + url, e);
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}

From source file:com.mycompany.horus.ServiceListener.java

private String getWsdlServicos() throws IOException {
    String resp;//  w w  w .  ja  v a  2s  . c  o  m
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(SERVICE_URL);
    HttpResponse response = httpclient.execute(post);
    HttpEntity respEntity = response.getEntity();
    resp = EntityUtils.toString(respEntity);
    return resp;
}