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:com.test.restdev.simpleRestClient.java

public static void main(String[] args) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://localhost:8080/restdev/resources/developers/rafi-kokos");
    HttpGet request1 = new HttpGet("http://localhost:8081/restdev/resources/developers/rafi-kokos");
    HttpGet request2 = new HttpGet("http://localhost:8082/restdev/resources/developers/rafi-kokos");
    HttpResponse response;//from  w ww.ja v  a 2s . c  o m
    for (int i = 0; i < 10; i++) {
        if (i % 3 == 0) {
            response = client.execute(request);
            System.out.println("If (i%3==0) -> call no. " + i + " to uri:" + request.getURI());
        } else if (i % 3 == 1) {
            response = client.execute(request1);
            System.out.println("If (i%3==1) -> call no. " + i + " to uri:" + request1.getURI());
        } else if (i % 3 == 2) {
            response = client.execute(request2);
            System.out.println("If (i%3==2) -> call no. " + i + " to uri:" + request2.getURI());
        } else {
            response = client.execute(request);
            System.out.println("Else -> call no. " + i + " to uri:" + request.getURI());
        }
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        System.out.println("call no. " + i);
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
    }

}

From source file:com.everm.httpclient.HttpClientTest.java

/**
 * @param args/* ww  w  .  j av a  2s .  co  m*/
 */
public static void main(String[] args) {
    String uri = "http://samsung-cps-000.s3.amazonaws.com/PDF/-137219737027956571";
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(uri);
    HttpResponse response = null;

    long startTime = System.currentTimeMillis();
    try {
        httpGet.addHeader("Date", "Mon, 18 Feb 2013 08:14:01 GMT");
        httpGet.addHeader("Authorization", "AWS AKIAIULRL33KVNJCCNAQ:ac4P+U1n+ib/PkOCqyuH4lt9hK8=");
        //         for (int i = 0; i < 100; i++) {
        response = client.execute(httpGet);
        //            System.out.println(i + "___" + response.getStatusLine());
        System.out.println(response.getStatusLine());
        EntityUtils.consume(response.getEntity());
        //         }
    } catch (Exception e) {
        e.printStackTrace();
    }

    long endTime = System.currentTimeMillis();
    System.out.println("elapsed time :" + (endTime - startTime));
}

From source file:MyTest.DownloadFileTest.java

public static void main(String args[]) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String url = "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7";
    System.out.println(url.charAt(50));
    HttpGet httpget = new HttpGet(url);
    HttpEntity entity = null;/*from w  w  w  .  j  a va 2s  .co  m*/
    try {
        HttpResponse response = httpclient.execute(httpget);
        entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            String filename = "testdata/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow";
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename)));
            int readedByte;
            while ((readedByte = bis.read()) != -1) {
                bos.write(readedByte);
            }
            bis.close();
            bos.close();
        }
        httpclient.close();
    } catch (IOException ex) {
        Logger.getLogger(DownloadFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:downloadwolkflow.httpTest.java

public final static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from   w w w.  j  av  a2s  . co m
        HttpGet httpget = new HttpGet(
                "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7");

        //System.out.println("Executing request " + httpget.getRequestLine());

        // Create a custom response handler
        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(final HttpResponse response)
                    throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }

        };
        String responseBody = httpclient.execute(httpget, responseHandler);
        System.out.println("----------------------------------------");
        System.out.println(responseBody);
    } finally {
        httpclient.close();
    }
}

From source file:ui.pack.MyFrame.java

public static void main(String... args) throws IOException {
    //MyFrame mf= new MyFrame();
    HttpClient client = HttpClientBuilder.create().build();
    String url = "http://localhost:8080/students/all";
    HttpGet get = new HttpGet(url);
    HttpResponse response = client.execute(get);
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()));
    StringBuilder builder = new StringBuilder();
    while (true) {
        String line = bufferedReader.readLine();
        if (line == null) {
            break;
        } else {/* w w  w. j  ava2 s.com*/
            builder.append(line);
        }
    }
    bufferedReader.close();
    String result = builder.toString();
    System.out.println(result);
    JSONArray arr = new JSONArray(result);
    System.out.println(arr.length());

    //client.
}

From source file:com.javaquery.apache.httpclient.HttpGetExample.java

public static void main(String[] args) {
    /* Create object of CloseableHttpClient */
    CloseableHttpClient httpClient = HttpClients.createDefault();

    /* Prepare get request */
    HttpGet httpGet = new HttpGet("http://www.example.com/api/customer");
    /* Add headers to get request */
    httpGet.addHeader("Authorization", "value");
    httpGet.addHeader("Content-Type", "application/json");

    /* Response handler for after request execution */
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

        @Override/*from  www  . ja v a2s . com*/
        public String handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
            /* Get status code */
            int httpResponseCode = httpResponse.getStatusLine().getStatusCode();
            System.out.println("Response code: " + httpResponseCode);
            if (httpResponseCode >= 200 && httpResponseCode < 300) {
                /* Convert response to String */
                HttpEntity entity = httpResponse.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                return null;
                /* throw new ClientProtocolException("Unexpected response status: " + httpResponseCode); */
            }
        }
    };

    try {
        /* Execute URL and attach after execution response handler */
        String strResponse = httpClient.execute(httpGet, responseHandler);
        /* Print the response */
        System.out.println("Response: " + strResponse);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.rest.samples.getImagePrintJPanel.java

public static void main(String[] args) {
    // TODO code application logic here
    String url = "https://api.adorable.io/avatars/eyes5";
    try {/*ww  w.  j a va2s.com*/
        HttpClient hc = HttpClientBuilder.create().build();
        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/png");
        HttpResponse res = hc.execute(getMethod);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        InputStream is = res.getEntity().getContent();

        Image image = ImageIO.read(is);
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

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

From source file:com.rest.samples.getImage.java

public static void main(String[] args) {
    // TODO code application logic here
    String url = "https://api.adorable.io/avatars/eyes1";
    try {/*  ww w  . j a va  2s  .c o m*/
        HttpClient hc = HttpClientBuilder.create().build();
        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/png");
        HttpResponse res = hc.execute(getMethod);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        InputStream is = res.getEntity().getContent();

        OutputStream os = new FileOutputStream(new File("img.png"));
        int read = 0;
        byte[] bytes = new byte[2048];
        while ((read = is.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        is.close();
        os.close();
    } catch (IOException ex) {
        Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.endava.webfundamentals.Main.java

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpRequest = new HttpGet("http://petstore.swagger.wordnik.com/api/api-docs");
    HttpResponse httpResponse = httpClient.execute(httpRequest);

    ObjectMapper objectMapper = new ObjectMapper();
    PetStore petStore = objectMapper.readValue(httpResponse.getEntity().getContent(), PetStore.class);

    PrintWriter out = new PrintWriter("PetStore.html");
    out.println("<html>");
    out.println("<header>");
    out.println(petStore.getInfo().getTitle());
    out.println("</header>");
    out.println("<body>");
    out.println("Api Version " + petStore.getApiVersion());
    out.println("Swagger Version " + petStore.getSwaggerVersion());
    out.println("<p>");
    out.println(petStore.getInfo().getDescription());
    out.println("</p>");
    out.println("<p>");
    out.println(petStore.getInfo().getContact());
    out.println("</p>");
    out.println(petStore.getInfo().getLicense());
    out.println(petStore.getInfo().getLicenseUrl());
    out.println("<p>");
    out.println(petStore.getInfo().getTermsOfServiceUrl());
    out.println("</p>");
    out.println("</body>");
    out.println("</html>");

    out.close();/* w  w w  . j  av  a2 s  .co  m*/
}

From source file:RestGetClient.java

public static void main(String[] args) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet("http://localhost:10080/example/json/product/get");
    getRequest.addHeader("accept", "application/json");

    HttpResponse response = httpClient.execute(getRequest);

    //if (response.getStatusLine().getStatusCode() != 200) {
    //   throw new RuntimeException("Failed : HTTP error code : "
    //      + response.getStatusLine().getStatusCode());
    //}/*  ww w .j  a v  a2 s. c  om*/

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

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();
}