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:Main.java

public static boolean logout() {
    String baseUrl = "http://10.6.8.2/cgi-bin/srun_portal?action=logout&ac_id=1";

    try {/*  w  w w . j  av a  2  s .  c  o m*/
        HttpGet getMethod = new HttpGet(baseUrl);
        getMethod.addHeader("Accept", "*/*");
        //getMethod.addHeader("Accept-Language", "zh-cn");
        //getMethod.addHeader("Referer", "http://202.117.2.41/index.html");
        //getMethod.addHeader("Content-Type", "application/x-www-form-urlencoded");
        //getMethod.addHeader("Accept-Encoding", "gzip, deflate");
        //getMethod.addHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)");
        getMethod.addHeader("Host", "10.6.8.2");
        //getMethod.addHeader("DNT", "1");

        HttpClient httpClient = new DefaultHttpClient();

        HttpResponse response = httpClient.execute(getMethod);
        Log.i(TAG, "Sending message.....");
        HttpEntity httpEntity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            String message = EntityUtils.toString(httpEntity);
            Log.i(TAG, "Logout succeed!!! message=" + message);
            return true;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.i(TAG, "Logout failed!!!");
    return false;
}

From source file:com.fufang.httprequest.HttpPostBuild.java

public static String postBuildJson(String url, String params)
        throws UnsupportedEncodingException, ClientProtocolException, IOException {

    CloseableHttpClient httpClient = HttpClients.createDefault();
    String postResult = null;//ww w .ja  v a  2  s.c om

    HttpPost httpPost = new HttpPost(url);
    System.out.println(" request " + httpPost.getRequestLine());

    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
            .build();
    httpPost.setConfig(requestConfig);
    StringEntity entity = new StringEntity(params.toString(), "UTF-8");
    entity.setContentType("application/json");
    httpPost.setEntity(entity);

    try {
        HttpResponse response = httpClient.execute(httpPost);
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity responseEntity = response.getEntity();
            postResult = EntityUtils.toString(responseEntity);
        } else {
            System.out.println("unexpected response status - " + status);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return postResult;
}

From source file:org.hobbit.spatiotemporalbenchmark.transformations.value.AddressFromNominatim.java

public static String getAddress(double latitude, double longitude) {
    String strAddress = "";
    String point = latitude + "," + longitude;

    String url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" + latitude + "&lon=" + longitude
            + "&addressdetails=1&accept-language=en";

    HttpPost httppost = new HttpPost(url);
    HttpClient httpclient = new DefaultHttpClient();
    String result = "";
    try {/* w w w  . j  a v a 2  s.  c o m*/
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntityGet = response.getEntity();
        if (resEntityGet != null) {
            result = EntityUtils.toString(resEntityGet);
        }
    } catch (Exception e) {
        System.out.println("Exception:" + e);
    }
    try {
        if (result != null) {
            JSONObject json = new JSONObject(result);
            strAddress = json.get("display_name").toString();
        }
    } catch (Exception e) {
    }
    try {
        if (!strAddress.equals("")) {
            pointAddressMap.put(point, strAddress);
            FileUtils.writeStringToFile(f, point + "=" + strAddress + "\n", true);
        }
    } catch (IOException ex) {
        Logger.getLogger(CoordinatesToAddress.class.getName()).log(Level.SEVERE, null, ex);
    }
    return strAddress;
}

From source file:com.google.appengine.tck.byteman.ConcurrentTxTestBase.java

protected static Thread execute(final CloseableHttpClient client, final HttpUriRequest request,
        final Holder holder) {
    Thread thread = new Thread(new Runnable() {
        public void run() {
            try {
                try (CloseableHttpResponse response = client.execute(request)) {
                    holder.out = EntityUtils.toString(response.getEntity());
                }/*www.  j  a  v  a2  s.  co m*/
            } catch (IOException ignore) {
            }
        }
    });
    thread.start();
    return thread;
}

From source file:org.hobbit.spatiotemporalbenchmark.transformations.value.AddressFromGoogleMapsApi.java

public static String getAddress(double latitude, double longitude) {
    String strAddress = "";
    String point = latitude + "," + longitude;
    String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude
            + "&sensor=true&language=en";

    HttpPost httppost = new HttpPost(url);
    HttpClient httpclient = new DefaultHttpClient();
    String result = "";
    try {//from ww w.  java 2 s.  c  om
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntityGet = response.getEntity();
        if (resEntityGet != null) {
            result = EntityUtils.toString(resEntityGet);
        }
    } catch (Exception e) {
        System.out.println("Exception:" + e);
    }
    try {
        /*Keeps the first address form the results*/
        if (result != null) {
            JSONObject json = new JSONObject(result);
            JSONArray ja = json.getJSONArray("results");
            //System.out.println(result.toString());
            for (int i = 0; i < 1; i++) {
                strAddress = ja.getJSONObject(i).getString("formatted_address");
            }
        }
    } catch (Exception e) {
    }
    try {
        if (!strAddress.equals("")) {
            pointAddressMap.put(point, strAddress);
            FileUtils.writeStringToFile(f, point + "=" + strAddress + "\n", true);
        }
    } catch (IOException ex) {
        Logger.getLogger(CoordinatesToAddress.class.getName()).log(Level.SEVERE, null, ex);
    }
    return strAddress;
}

From source file:middleware.HTTPRequest.java

public static List<Vuelo> doGetVuelos() throws IOException {
    String result;/*from w ww. j ava  2s  .  c o m*/
    String url = "http://localhost:8084/MVIv2/webapi/vuelos";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet(url);
    getRequest.addHeader("Accept", "application/json");

    HttpResponse response = httpClient.execute(getRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("ERROR AL CONSULTAR DEL TIPO: " + response.getStatusLine().getStatusCode());
    }

    HttpEntity entity = response.getEntity();
    result = EntityUtils.toString(entity);
    List<Vuelo> listaVuelos = getArrayVuelo(result);

    httpClient.getConnectionManager().shutdown();
    return listaVuelos;
}

From source file:org.hobbit.spatiotemporalbenchmark.transformations.value.AddressFromFoursquareApi.java

public static String getAddress(double latitude, double longitude) {
    String strAddress = "";
    String point = latitude + "," + longitude;

    String url = "https://api.foursquare.com/v2/venues/search?ll=" + latitude + "," + longitude
            + "&oauth_token=5TJR4WQZSOW0ZWTE4ENMXKO3Y415252GITEMRPQIVPMEGCYK&v=20120723&limit=1&accept-language=en";

    HttpPost httppost = new HttpPost(url);
    HttpClient httpclient = new DefaultHttpClient();
    String result = "";
    try {// w  w w.  j a  v a2 s.  c  o  m
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntityGet = response.getEntity();
        if (resEntityGet != null) {
            result = EntityUtils.toString(resEntityGet);
        }
    } catch (Exception e) {
        System.out.println("Exception:" + e);
    }
    try {
        /*Keeps the first address form the results*/
        if (result != null) {
            JSONObject json = new JSONObject(result);
            JSONObject jo = json.getJSONObject("response");
            JSONArray ja = jo.getJSONArray("venues");
            for (int i = 0; i < 1; i++) {
                strAddress = ja.getJSONObject(i).getJSONObject("location").getJSONArray("formattedAddress")
                        .toString();
                strAddress = strAddress.replace("},{", ",").replace("[", "").replace("]", "").replace("\"", "");
            }
        }
    } catch (Exception e) {
    }
    try {
        if (!strAddress.equals("")) {
            pointAddressMap.put(point, strAddress);
            FileUtils.writeStringToFile(f, point + "=" + strAddress + "\n", true);
        }
    } catch (IOException ex) {
        Logger.getLogger(CoordinatesToAddress.class.getName()).log(Level.SEVERE, null, ex);
    }
    return strAddress;
}

From source file:lets.code.project.conectividad.ConectivityClass.java

public static String getHTMLPage() {
    String str = "***";

    try {//from   ww  w.jav  a2 s. c o m
        HttpClient hc = new DefaultHttpClient();
        //HttpPost post = new HttpPost("http://www.yahoo.com");
        HttpGet get = new HttpGet("http://barrapunto.com/index.xml");

        HttpResponse rp = hc.execute(get);
        System.out.println("STATUS " + rp.getStatusLine().getStatusCode());
        if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            str = EntityUtils.toString(rp.getEntity());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return str;
}

From source file:com.handlerexploit.news.data.YQLHelper.java

public static String query(String query) {
    String fullUrl = null;/*  ww  w.  ja  v  a 2  s.c  om*/
    try {
        fullUrl = "http://query.yahooapis.com/v1/public/yql?format=json&q="
                + URLEncoder.encode(query, "US-ASCII");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Error encoding URL", e);
    }
    if (fullUrl != null) {
        String queryResponse = null;
        try {
            String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            AndroidHttpClient androidHttpClient = AndroidHttpClient.newInstance(userAgent); // TODO Look into using something else for pre-froyo
            queryResponse = EntityUtils.toString(androidHttpClient.execute(new HttpGet(fullUrl)).getEntity());
            androidHttpClient.close();
            JSONObject jsonQuery = new JSONObject(queryResponse).getJSONObject("query");
            if (jsonQuery.getInt("count") > 0) {
                return jsonQuery.getJSONObject("results").toString();
            } else {
                Log.d(TAG, "YQL returned empty - " + fullUrl + " - " + jsonQuery.toString());
            }
        } catch (Throwable e) {
            Log.e(TAG, "An error occured while parsing yql query - " + queryResponse, e);
        }
    }
    return null;
}

From source file:com.zhaosen.util.HttpClientUtil.java

License:asdf

@SuppressWarnings({ "rawtypes", "unchecked" })
public static String httpPost(String url, String param) throws ClientProtocolException, IOException {
    new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    ArrayList params = new ArrayList();
    params.add(new BasicNameValuePair("data", param));
    httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
    HttpResponse response = (new DefaultHttpClient()).execute(httpPost);
    if (response.getStatusLine().getStatusCode() == 200) {
        String result = EntityUtils.toString(response.getEntity());
        return result;
    } else {//from www.  jav a 2s  . c om
        return "";
    }
}