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.mxhero.plugin.cloudstorage.onedrive.api.command.ApiException.java

/**
 * Instantiates a new api exception./*from w w  w .  j  a va  2  s.c  o m*/
 *
 * @param response the response
 */
public ApiException(HttpResponse response) {
    super(response.getStatusLine().toString());
    this.reasonPhrase = response.getStatusLine().toString();
    this.statusCode = response.getStatusLine().getStatusCode();
    try {
        JsonNode content = OneDrive.JACKSON.readTree(EntityUtils.toString(response.getEntity()));
        if (content.has("error")) {
            this.error = OneDrive.JACKSON.treeToValue(content.get("error"), ResponseError.class);
        }
    } catch (Exception e) {
        logger.debug("error reading error response", e);
    }
}

From source file:org.brunocvcunha.instagram4j.requests.InstagramGetRequest.java

@Override
public T execute() throws ClientProtocolException, IOException {
    HttpGet get = new HttpGet(InstagramConstants.API_URL + getUrl());
    get.addHeader("Connection", "close");
    get.addHeader("Accept", "*/*");
    get.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    get.addHeader("Cookie2", "$Version=1");
    get.addHeader("Accept-Language", "en-US");
    get.addHeader("User-Agent", InstagramConstants.USER_AGENT);

    HttpResponse response = api.getClient().execute(get);
    api.setLastResponse(response);/*w  w  w. j av  a2  s  .com*/

    int resultCode = response.getStatusLine().getStatusCode();
    String content = EntityUtils.toString(response.getEntity());

    get.releaseConnection();

    return parseResult(resultCode, content);
}

From source file:org.elasticsearch.multi_node.RollupIT.java

static Map<String, Object> toMap(Response response) throws IOException {
    return toMap(EntityUtils.toString(response.getEntity()));
}

From source file:se.chalmers.watchme.net.HttpRetriever.java

/**
 * Send a GET request to a URL./*w w  w. j a va 2  s  .c  o m*/
 * 
 * <p>
 * The <code>url</code> parameter must be well formatted, as no validations
 * are made in this method.
 * </p>
 * 
 * @param url
 *            The url
 * @return String with the response on success, otherwise null
 * @throws {@link IOException} if the HTTP connection failed
 * @throws NoEntityException
 *             if no HttpEntity was available in the response
 */
public String get(String url) throws IOException, NoEntityException {

    HttpGet request = new HttpGet(url);

    HttpResponse res = this.client.execute(request);
    final int statusCode = res.getStatusLine().getStatusCode();

    Log.i("Custom", "Retrieving " + url + ", status: " + statusCode);

    // Return null if the status code isn't 200 OK
    if (statusCode != HttpStatus.SC_OK) {
        return null;
    }

    HttpEntity entity = res.getEntity();

    // Return a stringified version of the response
    if (entity != null) {
        return EntityUtils.toString(entity);
    } else {
        throw new NoEntityException("No entity from " + url);
    }
}

From source file:com.lonepulse.robozombie.response.PlainDeserializer.java

/**
 * <p>Deserializes the response content to a type which is assignable to {@link CharSequence}.</p>
 * //from   w  w w . j a  v a2  s  .com
 * @see AbstractDeserializer#run(InvocationContext, HttpResponse)
 */
@Override
public CharSequence deserialize(InvocationContext context, HttpResponse response) {

    try {

        HttpEntity entity = response.getEntity();
        return entity == null ? "" : EntityUtils.toString(entity);
    } catch (Exception e) {

        throw new DeserializerException(new StringBuilder("Plain deserialization failed for request <")
                .append(context.getRequest().getName()).append("> on endpoint <")
                .append(context.getEndpoint().getName()).append(">").toString(), e);
    }
}

From source file:com.klarna.checkout.Handler.java

/**
 * Handle response and don't throw exception for 3xx codes.
 *
 * @param response Response from HTTP Request
 *
 * @return HttpResponse object/*from  w w w.ja v  a 2s.  c o  m*/
 *
 * @throws IOException in case of a problem or the connection was aborted
 */
public HttpResponse handleResponse(final HttpResponse response) throws IOException {

    this.verifyStatusCode(response);

    String payload = EntityUtils.toString(response.getEntity());

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        parsePayload(payload);
    }

    return response;
}

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

License:asdf

@Test
public void testDescribe() throws Exception {
    final HttpGet method = new HttpGet(serverAddress + "describe");
    method.addHeader("Accept", TEXT_XML);
    final HttpResponse response = client.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    final String description = EntityUtils.toString(response.getEntity());
    logger.debug("Found the repository description:\n" + description);
    assertTrue("Failed to find a proper repo version",
            compile("<repositoryVersion>.*?</repositoryVersion>").matcher(description).find());
}

From source file:com.qihoo.permmgr.util.i.java

@Override
public void run() {
    Log.e("MyLog", Thread.currentThread().getName() + "--" + this.b + "---" + this.d
            + "---" + "---" + this.c);
    try {//from   w w w  . j  a v  a2s. c o m
        this.c = "http://api.shuaji.360.cn/c/getSolution?model=2013022&target=1&buildno=3.4.5&version=4.2.1&platform=mtk&pkg=2004&mid=fcc62dd788bfaf3bc53abf61cae9708d&new=1&src=1";
        HttpGet localHttpGet2 = new HttpGet(this.c);
        HttpResponse localHttpResponse = new DefaultHttpClient().execute(localHttpGet2);
        int statusCode = localHttpResponse.getStatusLine().getStatusCode();
        String result = EntityUtils.toString(localHttpResponse.getEntity());
        Log.e("MyLog", result);
        Log.e("MyLog", AESUtils.b(result));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:csiro.pidsvc.helper.Http.java

public static String simpleGetRequest(String uri) {
    HttpClient httpClient = new DefaultHttpClient();
    try {//from  w  ww. jav a2 s  .  c o m
        HttpGet httpGet = new HttpGet(uri);

        // Get the data.
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();

        // Return content.
        return EntityUtils.toString(entity);
    } catch (Exception e) {
        _logger.warn("Exception occurred while executing an HTTP request.", e);
        return null;
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.opencastproject.remotetest.util.WorkflowUtils.java

/**
 * Loads the workflow with id <code>workflowId</code> from the workflow service and returns the workflow instance as
 * an xml string.//w ww.  j  a va2  s .  c  o  m
 * 
 * @param workflowId
 *          the workflow identifier
 * @return the workflow instance xml
 * @throws IllegalStateException
 *           if the workflow doesn't exist or the workflow endpoint did not respond properly
 * @throws IOException
 *           if reading the response fails
 */
public static String getWorkflowById(String workflowId) throws IllegalStateException, IOException {
    HttpGet getWorkflowMethod = new HttpGet(BASE_URL + "/workflow/instance/" + workflowId + ".xml");
    TrustedHttpClient client = Main.getClient();
    HttpResponse response = client.execute(getWorkflowMethod);
    if (response.getStatusLine().getStatusCode() != 200)
        throw new IllegalStateException(EntityUtils.toString(response.getEntity()));
    String workflow = EntityUtils.toString(response.getEntity());
    return workflow;
}