Example usage for org.apache.http.client.methods HttpGet HttpGet

List of usage examples for org.apache.http.client.methods HttpGet HttpGet

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpGet HttpGet.

Prototype

public HttpGet(final String uri) 

Source Link

Usage

From source file:myexamples.MyExamples.java

public static void getExample() throws IOException {
    HttpGet httpGet = new HttpGet("http://localhost:9200/gb/tweet/9");
    CloseableHttpResponse response1 = httpclient.execute(httpGet);
    // The underlying HTTP connection is still held by the response object
    // to allow the response content to be streamed directly from the network socket.
    // In order to ensure correct deallocation of system resources
    // the user MUST call CloseableHttpResponse#close() from a finally clause.
    // Please note that if response content is not fully consumed the underlying
    // connection cannot be safely re-used and will be shut down and discarded
    // by the connection manager.
    try {//  w  w w  . j  a  v a 2  s .  c  o  m
        System.out.println(response1.getStatusLine());
        HttpEntity entity1 = response1.getEntity();
        // do something useful with the response body
        // and ensure it is fully consumed
        if (entity1 != null) {
            long len = entity1.getContentLength();
            if (len != -1 && len < 2048) {
                System.out.println(EntityUtils.toString(entity1));
            } else {
                System.out.println("entity length=" + len);
            }
        }

        EntityUtils.consume(entity1);
    } finally {
        response1.close();
    }
}

From source file:Main.java

public static String getHttpClientString(String path) {
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParams);
    DefaultHttpClient httpClient = (DefaultHttpClient) client;
    HttpResponse httpResponse = null;/* w w w. ja v  a  2  s .  co  m*/
    String result = "";
    try {
        httpResponse = httpClient.execute(new HttpGet(path));
        int res = httpResponse.getStatusLine().getStatusCode();
        if (res == 200) {
            InputStream in = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line.trim());
            }
            reader.close();
            in.close();
            result = sb.toString();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:core.RESTCalls.RESTGet.java

public static String httpGet(String urlStr) throws Exception {

    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet(urlStr);

    HttpResponse response = client.execute(get);

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

    String content = "", line = null;

    while ((line = rd.readLine()) != null)
        content += line + "\n";

    return content;
}

From source file:professionalpractice3.GETClient.java

static String updateStocks(String customerID, String stockSymbol, int shares, String price)
        throws ClientProtocolException, IOException {
    BufferedReader responseBody = null;
    HttpClient client = HttpClientBuilder.create().build();

    try {//from  w  ww . j  a  v  a2  s  . c o m
        //Define a HttpGet request
        HttpGet request = new HttpGet("http://" + localServer);

        //Set Http Headers
        request.addHeader("Accept", "application/xml");
        request.addHeader("Type", "update stocks");
        request.addHeader("Customer", customerID);
        request.addHeader("Symbol", stockSymbol);
        request.addHeader("Shares", Integer.toString(shares));
        request.addHeader("Price", price);

        //Invoke the service
        HttpResponse response = client.execute(request);

        //Verify if the response is valid
        int statusCode = response.getStatusLine().getStatusCode();
        //System.out.println(statusCode);
        if (statusCode != 200) {
            throw new RuntimeException("Failed with HTTP error code .... : " + statusCode);
        } else {
            //If valid, get the response
            responseBody = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = responseBody.readLine();
            return line;
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Invalid input");
        return "";
    } finally {
        if (responseBody != null)
            responseBody.close();
    }
}

From source file:com.devdungeon.httpexamples.DownloadFile.java

private static void download(String url, String filepath) {
    HttpClient client = new DefaultHttpClient();

    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 1000 * 5);
    HttpConnectionParams.setSoTimeout(params, 1000 * 5);

    HttpGet request = new HttpGet(url);

    HttpResponse response = null;/*w  ww. j  a v a 2 s . c om*/
    try {
        response = client.execute(request);
    } catch (IOException ex) {
        Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedReader rd;
    String line = null;
    try {
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        FileWriter fileWriter = new FileWriter(filepath);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        while ((line = rd.readLine()) != null) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedWriter.close();

    } catch (IOException ex) {
        Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedOperationException ex) {
        Logger.getLogger(HttpGetExample.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.opencastproject.remotetest.server.resource.InspectionResources.java

public static HttpResponse inspect(TrustedHttpClient client, String url) throws Exception {
    return client.execute(new HttpGet(getServiceUrl() + "inspect?url=" + url));
}

From source file:org.opencastproject.remotetest.server.resource.CaptureAdminResources.java

public static HttpResponse agent(TrustedHttpClient client, String id) {
    return client.execute(new HttpGet(getServiceUrl() + "agents/" + id));
}

From source file:cloud4all.Utils.NetworkAnode.java

public static void sendRequest(String anode) {
    AsyncTask<String, Void, Void> at = new AsyncTask<String, Void, Void>() {
        @Override/*from  w  ww. ja v  a 2  s.  co m*/
        protected Void doInBackground(String... url) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet command = new HttpGet(url[0]);
                HttpResponse response = client.execute(command);

                InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
                BufferedReader in = new BufferedReader(isr);
                String res = in.readLine();
                Log.d("GPIIUserListeners", "GPII has returned: " + res);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    at.execute(anode);
}

From source file:org.opencastproject.remotetest.server.resource.AdminResources.java

public static HttpResponse recordingsUpcoming(TrustedHttpClient client) {
    return client.execute(new HttpGet(getServiceUrl() + "recordings/upcoming"));
}

From source file:HttpUtils.java

public static InputStream getData(String url) throws ClientProtocolException, IOException {
    HttpGet httpget = new HttpGet(url);
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(httpget);
    if (response.getStatusLine().getStatusCode() != 200)
        return null;

    HttpEntity entity = response.getEntity();
    if (entity == null) {
        return null;
    }/* ww w . java2 s .  c  o m*/
    InputStream instream = entity.getContent();
    return instream;
}