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:de.taimos.httputils.WS.java

/**
 * @param response the {@link HttpResponse}
 * @return String the body as UTF-8 string
 *//* www.ja  va  2  s.c om*/
public static String getResponseAsString(final HttpResponse response) {
    try {
        return EntityUtils.toString(response.getEntity(), "UTF-8");
    } catch (final ParseException e) {
        throw new RuntimeException(e);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.iiordanov.wiktionarylookup.util.HttpUtil.java

public static String curl(String url) {
    String html = null;/*from  w ww  . j  a v a2s. com*/

    try {
        HttpUriRequest get = new HttpGet(url);
        HttpResponse response = new DefaultHttpClient().execute(get);
        html = EntityUtils.toString(response.getEntity(), "utf-8");
    } catch (ClientProtocolException e) {
        //Log.e (Global.TAG, "Could not curl", e);
    } catch (IOException e) {
        //Log.e (Global.TAG, "Could not curl", e);
    }

    return html;
}

From source file:coolmapplugin.util.HTTPRequestUtil.java

public static String executePost(String targetURL, String jsonBody) {

    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {

        HttpPost request = new HttpPost(targetURL);

        StringEntity params = new StringEntity(jsonBody);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);//from w ww.  jav  a2 s .  com

        HttpResponse result = httpClient.execute(request);

        HttpEntity entity = result.getEntity();
        if (entity == null)
            return null;

        String jsonResult = EntityUtils.toString(result.getEntity(), "UTF-8");

        return jsonResult;

    } catch (IOException e) {
        return null;
    }
}

From source file:com.leprechaun.solveig.http.RequestExecutor.java

public static ResponseEntity simpleExecutor(String host, String port, String query) {

    ResponseEntity responseEntity = new ResponseEntity();

    HttpClient client = new DefaultHttpClient();

    String url = "http://" + host + ":" + port + "/query?q=" + query;

    System.out.println(url);/*from ww  w. j  av a  2  s.  co m*/

    HttpGet get = new HttpGet(url);

    try {
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);

        responseEntity.setCode(response.getStatusLine().toString());
        responseEntity.setBody(responseString);
    } catch (IOException e) {
        System.out.println(e.getMessage());

        responseEntity.setCode("ERROR");
        responseEntity.setBody(e.getMessage());
    }

    return responseEntity;
}

From source file:com.mehmetakiftutuncu.eshotroid.utilities.Request.java

public static String get(String url) {
    try {/* w  ww.j  a v  a2 s. c om*/
        Log.info(Request.class, "Making GET request to url: " + url);

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode != 200) {
            Log.error(Request.class,
                    "GET request failed, invalid status code! status code: " + statusCode + ", url: " + url);
            return null;
        } else {
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");

            Log.info(Request.class, "GET result from url: " + url);
            Log.info(Request.class, result);

            return result;
        }
    } catch (Exception e) {
        Log.error(Request.class, "GET request failed! url: " + url, e);
        return null;
    }
}

From source file:de.escidoc.core.test.EntityUtil.java

public static String toString(HttpEntity entity, String charEncoding) throws IOException {
    if (entity == null) {
        return "";
    }//from w w  w  . j av a2s .c o  m
    return EntityUtils.toString(entity, charEncoding);
}

From source file:org.plos.crepo.util.HttpResponseUtil.java

public static String getResponseAsString(HttpResponse response) {
    HttpEntity entity = response.getEntity();
    try {// ww w  .  j  a v a 2 s.  co m
        return EntityUtils.toString(entity, CharEncoding.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

static public String doHttpGet(String url, HashMap<String, String> map) {

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
    String result = "ERROR";
    if (null != map) {
        int i = 0;
        for (Map.Entry<String, String> entry : map.entrySet()) {

            if (i == 0) {
                url = url + "?" + entry.getKey() + "=" + entry.getValue();
            } else {
                url = url + "&" + entry.getKey() + "=" + entry.getValue();
            }/*  ww  w  .  jav  a 2 s . c  om*/

            i++;

        }
    }
    HttpGet get = new HttpGet(url);
    get.setHeaders(headers);
    try {

        HttpResponse response = client.execute(get);

        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // setCookie(response);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");

        } else {
            result = EntityUtils.toString(response.getEntity(), "UTF-8")
                    + response.getStatusLine().getStatusCode() + "ERROR";
        }
        if (result != null) {
            if (result.startsWith("\ufeff")) {
                result = result.substring(1);
            }
        }

    } catch (ConnectTimeoutException e) {
        result = "TIMEOUTERROR";
    }

    catch (Exception e) {
        result = "OTHERERROR";
        e.printStackTrace();

    }

    return result;
}

From source file:HelloWorld.BaseTest.java

public <T> T responseToEntity(CloseableHttpResponse response, Class<T> type) throws IOException {
    Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy").create();
    return gson.fromJson(EntityUtils.toString(response.getEntity(), "UTF-8"), type);
}

From source file:com.ssy.havefunweb.util.WeixinUtil.java

public static JSONObject doGetStr(String url) throws IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    JSONObject jsonObject = null;/*from   w  ww  .ja  v  a 2 s  .  c  om*/
    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        String result = EntityUtils.toString(entity, "UTF-8");
        jsonObject = JSONObject.fromObject(result);
    }
    return jsonObject;
}