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

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

    try {/*w ww  . java  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.sematext.ag.http.HttpUtils.java

public static boolean processRequestSilently(HttpClient httpClient, HttpRequestBase request) {
    try {/* w ww  .j a va 2s .  c o m*/
        HttpResponse response = httpClient.execute(request);
        LOG.info("Event sent");
        if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) {
            return false;
        }
        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);
        return true;
    } catch (IOException e) {
        LOG.error("Sending event failed", e);
        return false;
    } finally {
        request.releaseConnection();
    }
}

From source file:LogToFile.java

private static HttpResponse ExecuteRequest(HttpUriRequest request) {
    try {//from w  w w . j a v a 2s .c  om
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(request);
        return response;
    } catch (Exception e) {
        Log.e("", e.getClass().getName() + "\n" + e.getMessage());
    }
    return null;
}

From source file:Main.java

public static boolean getkeepaliveinfo() {
    String baseUrl = "http://10.6.8.2/cgi-bin/keeplive?";

    try {/*from   w w  w  .ja v a  2s.  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);
            if (httpEntity == null || message.compareTo("error") == 0) {
                Log.i(TAG, "Get keepalive info failed!!!message=" + message);
                return false;
            } else {
                Log.i(TAG, "Get keepalive info succeed!!!message=" + message);
                return true;
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Log.i(TAG, "Get keepalive info failed!!!");
    return false;
}

From source file:server.web.HttpRequestHelper.java

private static String send(HttpGet request) throws IOException {
    HttpClient client = getHttpClient();
    HttpResponse response = client.execute(request);

    log.info("Response Code : " + response.getStatusLine().getStatusCode());
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuilder result = new StringBuilder();
    String line;//w w w  .  j a  va2 s .  c  om

    // !!!!! head'    VM ?
    while ((line = rd.readLine()) != null) {
        log.info("line: " + line);
        result.append(line);
    }

    log.info("Response: " + result.toString());

    return result.toString();
}

From source file:Main.java

public static String responseContentUri(URI uri) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost();
    request.setURI(uri);//from ww  w.j a va2 s.com
    InputStream is = client.execute(request).getEntity().getContent();
    BufferedReader inb = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder("");
    String line;
    String NL = System.getProperty("line.separator");
    while ((line = inb.readLine()) != null) {
        sb.append(line).append(NL);
    }
    inb.close();
    return sb.toString();
}

From source file:Main.java

public static String getInputStreamFromUrl(String url) {
    InputStream content = null;/*  www  . j a v  a 2  s . c o  m*/
    String res = "";
    try {
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpclient = new DefaultHttpClient();
        // Execute HTTP Get Request
        HttpResponse response = httpclient.execute(httpGet);
        content = response.getEntity().getContent();
        res = InputStreamtoString(content);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return res;
}

From source file:com.lushell.tc.dbpaas.api.utils.MyHttpUtil.java

public static String getDataFromUrl(String url, int timeout) {
    try {// w ww .j  a v a  2s  .com
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();
        //StatusLine statusLine = response.getStatusLine();
        String body = EntityUtils.toString(entity);
        return body;
    } catch (IOException ex) {
        Logger.getLogger(MyHttpUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:com.huguesjohnson.retroleague.util.RestInvoke.java

public static String invoke(String restUrl) throws Exception {
    String result = null;/*from  w w w . j  a v  a 2  s.  c o m*/
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(restUrl);
    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity httpEntity = response.getEntity();
    if (httpEntity != null) {
        InputStream in = httpEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuffer temp = new StringBuffer();
        String currentLine = null;
        while ((currentLine = reader.readLine()) != null) {
            temp.append(currentLine);
        }
        result = temp.toString();
        in.close();
    }
    return (result);
}

From source file:Main.java

public static String responseContent(String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(url));
    InputStream is = client.execute(request).getEntity().getContent();
    BufferedReader inb = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder("");
    String line;//from ww  w .  java2s  . c  o m
    String NL = System.getProperty("line.separator");
    while ((line = inb.readLine()) != null) {
        sb.append(line).append(NL);
    }
    inb.close();
    return sb.toString();
}