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, String str) throws IOException, ParseException 

Source Link

Usage

From source file:com.macrossx.wechat.http.WechatHttpClient.java

public String send(final HttpUriRequest request) {
    String result = "";
    try {//from   ww w  .j a v a2s  .c  o  m
        CloseableHttpResponse response = HttpClients.createDefault().execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK != statusCode) {
            log.info("Failed to get!");
            return "";
        }
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity, "utf-8");
    } catch (IOException e) {
        log.info(e.getMessage());
    }
    return result;
}

From source file:de.escidoc.core.test.sm.ReportTestBase.java

/**
 * Test retrieving an report from the mock framework.
 *
 * @param xml Treport-parameters xml.//  w ww. ja v  a 2 s . c  o  m
 * @return The retrieved report.
 * @throws Exception If anything fails.
 */
public String retrieve(final String xml) throws Exception {

    Object result = getReportClient().retrieve(xml);
    String xmlResult = null;
    if (result instanceof HttpResponse) {
        HttpResponse httpRes = (HttpResponse) result;
        xmlResult = EntityUtils.toString(httpRes.getEntity(), HTTP.UTF_8);
        assertHttpStatusOfMethod("", httpRes);
    } else if (result instanceof String) {
        xmlResult = (String) result;
    }
    return xmlResult;
}

From source file:AIR.Dictionary.DictionaryConnection.java

public String lookup(String word) {

    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        //        // make word url safe
        word = UrlEncoderDecoderUtils.encode(word);

        // create url to send
        String baseUrl = _apiUrl + word;
        URI restAPIURI = new URI(baseUrl + "?key=" + _apiKey);
        // create client
        HttpGet request = new HttpGet(restAPIURI);
        request.setHeader("Accept-Charset", "UTF-8");
        HttpResponse response = httpClient.execute(request);

        String responseXML = EntityUtils.toString(response.getEntity(), "UTF-8");
        return responseXML;

    } catch (Exception e) {
        _logger.error("Error Calling Dictionary rest API ", e);
    }/*  w  ww.  ja v  a2s  . c o m*/
    return null;
}

From source file:com.pyz.tool.weixintool.util.HttpTool.java

/**
 * get  url ?/*from   w w  w  .  ja  v a  2 s. c  o m*/
 * @param url
 * @return
 * @throws Exception
 */
public static String request(String url) throws Exception {
    String result = "";
    HttpClient httpClient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(url);
        HttpResponse httpresponse = httpClient.execute(httpget);
        HttpEntity entity = httpresponse.getEntity();
        result = EntityUtils.toString(entity, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:de.escidoc.core.test.sm.StatisticDataTestBase.java

/**
 * Test creating statistic data.//  w w w .  j a va  2 s  .c  o  m
 *
 * @param dataXml The xml representation of the item.
 * @return The created item.
 * @throws Exception If anything fails.
 */
public String create(final String dataXml) throws Exception {

    Object result = getStatisticDataClient().create(dataXml);
    String xmlResult = null;
    if (result instanceof HttpResponse) {
        HttpResponse httpRes = (HttpResponse) result;
        xmlResult = EntityUtils.toString(httpRes.getEntity(), HTTP.UTF_8);
        assertHttpStatusOfMethod("", httpRes);

    } else if (result instanceof String) {
        xmlResult = (String) result;
    }
    return xmlResult;
}

From source file:org.elasticsearch.test.rest.client.RestTestResponse.java

public RestTestResponse(Response response) throws IOException {
    this.response = response;
    if (response.getEntity() != null) {
        try {/*from w w w . j  a va  2  s .c o  m*/
            this.body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeWhileHandlingException(response);
        }
    } else {
        this.body = null;
    }
    parseResponseBody();
}

From source file:com.ibm.watson.app.common.util.rest.JSONResponseHandler.java

@Override
protected T handleEntity(HttpEntity entity) throws IOException {
    final String jsonString = EntityUtils.toString(entity, CHARSET);
    if (logger.isTraceEnabled()) {
        logger.trace("Response JSON: " + jsonString);
    }//from  w w  w  . j  av  a 2s  .  com

    if (jsonString == null || jsonString.isEmpty()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Received null or empty JSON string '" + jsonString + "'");
        }
        return getDefaultReturnValue();
    }

    try {
        T retval = parseJSON(jsonString);
        validate(retval);
        return retval;
    } catch (JsonSyntaxException e) {
        logger.error(MessageKey.AQWEGA04001E_unable_parse_json_1.getMessage(e.getMessage()));
        logger.catching(e);
        return getDefaultReturnValue();
    }
}

From source file:org.wso2.carbon.integration.test.client.HttpEventReceiverClient.java

public void receive(String url, String methodType) {

    receivedMessage = null;//from   w w w.j a v a2  s.  c o m
    final String urlValue = url;

    try {
        // Create an instance of HttpClient.
        HttpClient client = new SystemDefaultHttpClient();
        HttpRequestBase method = null;

        if (methodType.equals("GET")) {
            method = new HttpGet(urlValue);
        } else {
            method = new HttpPost(urlValue);
        }

        HttpResponse httpResponse = client.execute(method);

        receivedMessage = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        log.info(receivedMessage);

    } catch (Throwable t) {
        log.error(t);
    }
}

From source file:com.agileapes.couteau.http.util.HttpEntityStringTransformer.java

@Override
public String map(HttpEntity input) {
    try {//  ww  w  . j a v a 2  s  . c  o  m
        return EntityUtils.toString(input, charset);
    } catch (Throwable e) {
        return null;
    }
}

From source file:net.nym.library.cookie.CookieTest.java

private String getRequest(Context context, String urlString, Map<String, Object> params) throws IOException {
    StringBuffer param = new StringBuffer();
    int i = 0;/*from  w w  w .ja v  a2s .  c o m*/
    for (String key : params.keySet()) {
        if (i == 0)
            param.append("?");
        else
            param.append("&");
        param.append(key).append("=").append(params.get(key));
        i++;
    }

    Log.i(urlString + param.toString());
    //URL?
    HttpGet getMethod = new HttpGet(urlString + param.toString());

    DefaultHttpClient client = new DefaultHttpClient();

    client.setCookieStore(new PersistentCookieStore(context));
    HttpResponse response = client.execute(getMethod);
    if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
        String result = EntityUtils.toString(response.getEntity(), "utf-8");
        return result;
    }
    return null;
}