Example usage for org.apache.http.client HttpClient execute

List of usage examples for org.apache.http.client HttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient execute.

Prototype

HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;

Source Link

Document

Executes HTTP request using the default context.

Usage

From source file:language_engine.HttpUtils.java

public static String doHttpGet(String url, Header[] headers) {
    try {//from   w  w w  . j  ava 2  s.c  om
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeaders(headers);
        HttpResponse resp = httpClient.execute(httpGet);

        return StreamUtil.readText(resp.getEntity().getContent(), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.travey.travey.DataConnection.java

public static String GET(String url) {
    InputStream inputStream = null;
    String result = "";
    try {//from ww w .  j a v  a  2  s  .c  o m
        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();
        // convert inputstream to string
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String HttpGet(String url) {
    HttpClient client = new DefaultHttpClient();
    StringBuilder builder = new StringBuilder();
    HttpGet myget = new HttpGet(url);
    try {/*  www  .  j a  va2 s  .  c  o m*/
        HttpResponse response = client.execute(myget);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
            builder.append(s);
        }

        char cr = 65279;
        String t = String.valueOf(cr);
        String resultString = builder.toString();
        resultString = resultString.replace("\t", "").replace(t, "");

        return resultString;

    } catch (Exception e) {
        Log.v("url response", "false");
        e.printStackTrace();
    }
    return null;
}

From source file:com.uber.stream.kafka.mirrormaker.common.utils.C3QueryUtils.java

private static String makeQuery(String c3Host, int c3Port, String query) throws IOException {
    String url = "http://" + c3Host + ":" + c3Port + DEFAULT_QUERY_PATH + query;
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    try {//from w w w . j av a2  s. c  om
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line;
        StringBuffer sb = new StringBuffer();
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } finally {
        if (response.getEntity() != null) {
            EntityUtils.consume(response.getEntity());
        }
    }
}

From source file:core.RESTCalls.RESTDelete.java

public static String httpDelete(String urlStr) {

    HttpClient client = new DefaultHttpClient();

    HttpDelete delete = new HttpDelete(urlStr);

    String result = null;/*from   ww  w  .ja  va  2  s  . c  o m*/

    try {

        HttpResponse response = client.execute(delete);

        if (response != null && response.getEntity() != null) {

            BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuilder total = new StringBuilder();

            String line = null;

            while ((line = r.readLine()) != null)
                total.append(line + "\n");

            result = total.toString();
        }
    } catch (Exception ex) {

        ex.printStackTrace();
    }

    return result;
}

From source file:markson.visuals.sitapp.JSONfunctions.java

public static JSONObject getJSONfromURL(String url) {

    //initialize/*www  .  j a va  2s  .c o m*/
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    //http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    //convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    //try parse the string to a JSON object
    try {
        jArray = new JSONObject(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }

    return jArray;
}

From source file:org.radiognu.radiognu.utils.utilsradiognu.java

public static final String getResponseServiceAPI(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;//from   www  . j ava2 s.c  o m
    String responseString = null;
    try {
        // make a HTTP request
        response = httpclient.execute(new HttpGet(uri[0]));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            // request successful - read the response and close the connection
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else {
            // request failed - close the connection
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (Exception e) {
    }
    return responseString;
}

From source file:org.codeqinvest.web.IntegrationTestHelper.java

private static void doPostRequest(String uri, List<NameValuePair> parameters) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost(uri);
    post.setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
    httpClient.execute(post);
}

From source file:gov.nasa.ensemble.common.http.HttpUtils.java

public static void execute(HttpClient client, HttpRequestBase request, HttpResponseHandler handler)
        throws Exception {
    HttpResponse response = null;/* w ww  .  jav  a 2s. c o m*/
    try {
        response = client.execute(request);
        if (handler != null) {
            handler.handleResponse(response);
        }
    } finally {
        HttpUtils.consume(request, response);
    }
}

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

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

    try {/*from   w w  w . j  a  v  a  2  s.  c om*/
        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;
}