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:com.huangyifei.etag.HttpClientWrapper.java

@Override
public String getRawKey() {
    String url = request.getURI().toString();
    String method = request.getMethod();
    Header[] headers = request.getAllHeaders();
    HttpEntity entity = null;//w w  w.  j a  va2 s . c om
    String entityString = null;
    if (request instanceof HttpEntityEnclosingRequestBase) {
        entity = ((HttpEntityEnclosingRequestBase) request).getEntity();
    }
    if (entity != null) {
        if (!(entity instanceof UrlEncodedFormEntity)) {
            return "";
        }
        if (!entity.isRepeatable()) {
            return "";
        }
        try {
            entityString = EntityUtils.toString(entity);
        } catch (ParseException e) {
            L.d("generateKey", e);
        } catch (IOException e) {
            L.d("generateKey", e);
        }
    }
    StringBuilder source = new StringBuilder(method);
    source.append('-');

    source.append(url);
    source.append('-');

    if (headers != null) {
        for (Header header : headers) {
            source.append(header.getName()).append(':').append(header.getValue());
        }
        source.append('-');
    }

    if (!TextUtils.isEmpty(entityString)) {
        source.append(entityString);
    }
    return source.toString();
}

From source file:ADP_Streamline.CURL2.java

public String uploadFiles(File file, String siteId, String containerId, String uploadDirectory) {

    String json = null;/* w w w.  j a v a2 s.co m*/
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpHost targetHost = new HttpHost("localhost", 8080, "http");

    try {

        HttpPost httppost = new HttpPost("/alfresco/service/api/upload?alf_ticket=" + this.ticket);

        FileBody bin = new FileBody(file);
        StringBody siteid = new StringBody(siteId);
        StringBody containerid = new StringBody(containerId);
        StringBody uploaddirectory = new StringBody(uploadDirectory);

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("filedata", bin);
        reqEntity.addPart("siteid", siteid);
        reqEntity.addPart("containerid", containerid);
        reqEntity.addPart("uploaddirectory", uploaddirectory);

        httppost.setEntity(reqEntity);

        //log.debug("executing request:" + httppost.getRequestLine());

        HttpResponse response = httpclient.execute(targetHost, httppost);

        HttpEntity resEntity = response.getEntity();

        //log.debug("response status:" + response.getStatusLine());

        if (resEntity != null) {
            //log.debug("response content length:" + resEntity.getContentLength());

            json = EntityUtils.toString(resEntity);
            //log.debug("response content:" + json);
        }

        EntityUtils.consume(resEntity);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    return json;
}

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

@Test
public void testIngestWithNew() throws Exception {
    final HttpPost method = postObjMethod("new");
    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());
}

From source file:com.fizzed.ninja.rocker.NinjaRockerIntegrationTest.java

@Test
public void chunkedTransferEncodingNotUsed() throws Exception {
    HttpResponse response = this.ninjaTestBrowser.makeRequestAndGetResponse(ninjaTestServer.getBaseUrl() + "/",
            new HashMap<String, String>());
    String body = EntityUtils.toString(response.getEntity());

    assertThat(body, containsString("Hi!"));
    assertThat(response.getFirstHeader("Transfer-Encoding"), is(nullValue()));
    assertThat(response.getFirstHeader("Content-Length"), is(not(nullValue())));
    assertThat(SwissKnife.convert(response.getFirstHeader("Content-Length").getValue(), Integer.class),
            greaterThan(0));/*w w w.j  a  v  a 2s . c om*/
}

From source file:es.uvigo.esei.dai.hybridserver.utils.TestUtils.java

/**
 * Realiza una peticin por GET y devuelve el contenido de la respuesta.
 * /*from  w ww . j a  v  a2 s .  co m*/
 * La peticin se hace en UTF-8 y solicitando el cierre de la conexin.
 * 
 * Este mtodo comprueba que el cdigo de respuesta HTTP sea 200 OK.
 * 
 * @param url URL de la pgina solicitada.
 * @return contenido de la respuesta HTTP.
 * @throws IOException en el caso de que se produzca un error de conexin.
 */
public static String getContentWithType(String url, String contentType) throws IOException {
    final HttpResponse response = Request.Get(url).addHeader("Connection", "close")
            .addHeader("Content-encoding", "UTF-8").execute().returnResponse();

    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(contentType, response.getEntity().getContentType().getValue());

    return EntityUtils.toString(response.getEntity());
}

From source file:com.vmware.bdd.plugin.clouderamgr.poller.host.handler.DefaultResponseHandler.java

@Override
public ParseResult handleResponse(HttpResponse response) throws IOException {

    StatusLine statusLine = response.getStatusLine();
    HttpEntity entity = response.getEntity();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }/*w  w w.  j  a  v  a2 s  .c om*/
    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }

    return contentParser.parse(EntityUtils.toString(entity));
}

From source file:org.openmobster.core.location.TestGoogleTestDrive.java

public void testPlacesRequest() throws Exception {
    log.info("Starting Google Place Search........");

    String apiKey = "AIzaSyAntv38LTmUTBSlSLHzX-XbfNFcl4F5zrA";
    String url = "https://maps.googleapis.com/maps/api/place/search/xml?location=-33.8670522,151.1957362"
            + "&radius=500&types=food&name=harbour&sensor=false&key=" + apiKey;

    //setup the request object
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);

    //send the request
    HttpResponse response = client.execute(request);

    //read the response
    String result = null;// www  .j av a2 s .  co  m
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        result = EntityUtils.toString(entity);
    }

    //process
    System.out.println(result);
}

From source file:org.restexpress.plugin.correlationid.CorrelationIdPluginTest.java

@Test
public void shouldGenerateCorrelationIdOnNull() throws Exception {
    HttpGet request = new HttpGet("http://localhost:8081/test");
    HttpResponse response = (HttpResponse) http.execute(request);
    HttpEntity entity = response.getEntity();
    String json = EntityUtils.toString(entity);
    assertNotNull(json);/*w  ww.j  a  v  a  2  s.  com*/
    UUID.fromString(json);
}