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:com.hilatest.httpclient.apacheexample.ClientAbortMethod.java

public final static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("http://www.apache.org/");

    System.out.println("executing request " + httpget.getURI());
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
    }/* w ww .j  av  a  2s  .c  om*/
    System.out.println("----------------------------------------");

    // Do not feel like reading the response body
    // Call abort on the request object
    httpget.abort();

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

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

public static void main(String[] args) {
    // TODO code application logic here
    //        String url = "https://api.adorable.io/avatars/list";
    String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5";
    try {/*  ww  w. jav  a 2  s .  c o m*/
        HttpClient hc = HttpClientBuilder.create().build();
        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/json");
        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();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(br);
        if (element.isJsonObject()) {
            JsonObject jsonObject = element.getAsJsonObject();
            Set<Map.Entry<String, JsonElement>> jsonEntrySet = jsonObject.entrySet();
            for (Map.Entry<String, JsonElement> entry : jsonEntrySet) {
                if (entry.getValue().isJsonArray() && entry.getValue().getAsJsonArray().size() > 0) {
                    JsonArray jsonArray = entry.getValue().getAsJsonArray();
                    Set<Map.Entry<String, JsonElement>> internalJsonEntrySet = jsonArray.get(0)
                            .getAsJsonObject().entrySet();
                    for (Map.Entry<String, JsonElement> entrie : internalJsonEntrySet) {
                        System.out.println("--->   " + entrie.getKey() + " --> " + entrie.getValue());

                    }

                } else {
                    System.out.println(entry.getKey() + " --> " + entry.getValue());
                }
            }
        }

        String output;
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

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

From source file:com.oeg.oops.Main.java

public static void main(String[] args) {
    System.out.println("Hello, World");

    for (int i = 0; i < args.length; i++) {
        System.out.println("arg: " + args[i]);
    }/* w w w .jav  a 2s  . co  m*/
    //        if(args.length<2){
    //            System.out.println("expect 2 arguments: vocabulary-uri and output-directory");
    //        }
    //        else{
    //            String uri = args[0];
    //            String output_dir = args[1];
    if (true) {
        String uri = "https://raw.githubusercontent.com/ahmad88me/demo/master/alo.owl";
        String output_dir = "./output";

        System.out.println("uri: " + uri);
        System.out.println("output dir: " + output_dir);

        try {

            HttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet(uri);
            request.setHeader("Accept", "application/rdf+xml");
            HttpResponse response = client.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                String contentType = response.getFirstHeader("Content-Type").getValue();
                System.out.println("content type: " + contentType);
            }
        } catch (Exception e) {
            System.err.println("Error while doing http get: " + " in " + uri + " " + e.getMessage());
        }

        //Vocabulary v = new Vocabulary(uri);
        //            CreateOOPSEvalPage coep = new CreateOOPSEvalPage(v);
        //            coep.createPage(output_dir);
    }

}

From source file:com.dlmu.heipacker.crawler.client.ClientAbortMethod.java

public final static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {// w  ww  .ja  va 2  s  .c o m
        HttpGet httpget = new HttpGet("http://www.apache.org/");

        System.out.println("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        System.out.println("----------------------------------------");

        // Do not feel like reading the response body
        // Call abort on the request object
        httpget.abort();
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.mtea.macrotea_httpclient_study.ClientAbortMethod.java

public final static void main(String[] args) throws Exception {
    //? DefaultHttpClient
    HttpClient httpclient = new DefaultHttpClient();
    try {//from   w  ww .j  a  v a2 s  .com
        HttpGet httpget = new HttpGet("http://www.baidu.com/");

        System.out.println("executing request " + httpget.getURI());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println("RequestLine:" + httpget.getRequestLine());
        System.out.println("RequestLine:" + response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        System.out.println("----------------------------------------");
        //
        EntityUtils.consume(entity);
        //??response?httpget
        httpget.abort();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //??httpclient???
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:edu.uci.ics.crawler4j.examples.login.LoginCrawlController.java

public static void main(String[] args) throws Exception {
    //        if (args.length != 2) {
    //            System.out.println("Needed parameters: ");
    //            System.out.println("\t rootFolder (it will contain intermediate crawl data)");
    //            System.out.println("\t numberOfCralwers (number of concurrent threads)");
    //            return;
    //        }/*from ww w . ja  v  a  2 s.co m*/

    /*
       * crawlStorageFolder is a folder where intermediate crawl data is
     * stored.
     */
    String crawlStorageFolder = "/tmp/test_crawler/";

    /*
     * numberOfCrawlers shows the number of concurrent threads that should
     * be initiated for crawling.
     */
    int numberOfCrawlers = 1;

    CrawlConfig config = new CrawlConfig();

    config.setCrawlStorageFolder(crawlStorageFolder);

    /*
     * Be polite: Make sure that we don't send more than 1 request per
     * second (1000 milliseconds between requests).
     */
    config.setPolitenessDelay(1000);

    /*
     * You can set the maximum crawl depth here. The default value is -1 for
     * unlimited depth
     */
    config.setMaxDepthOfCrawling(0);

    /*
     * You can set the maximum number of pages to crawl. The default value
     * is -1 for unlimited number of pages
     */
    config.setMaxPagesToFetch(1000);

    /*
     * Do you need to set a proxy? If so, you can use:
     * config.setProxyHost("proxyserver.example.com");
     * config.setProxyPort(8080);
     *
     * If your proxy also needs authentication:
     * config.setProxyUsername(username); config.getProxyPassword(password);
     */

    /*
     * This config parameter can be used to set your crawl to be resumable
     * (meaning that you can resume the crawl from a previously
     * interrupted/crashed crawl). Note: if you enable resuming feature and
     * want to start a fresh crawl, you need to delete the contents of
     * rootFolder manually.
     */
    config.setResumableCrawling(false);

    config.setIncludeHttpsPages(true);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(new HttpGet("http://58921.com/user/login"));

    HttpEntity entity = response.getEntity();
    String content = EntityUtils.toString(entity, HTTP.UTF_8);
    Document doc = Jsoup.parse(content);
    Elements elements = doc.getElementById("user_login_form").children();
    Element tokenEle = elements.last();
    String token = tokenEle.val();
    System.out.println(token);
    LoginConfiguration somesite;
    try {
        somesite = new LoginConfiguration("58921.com", new URL("http://58921.com/user/login"),
                new URL("http://58921.com/user/login/ajax?ajax=submit&__q=user/login"));
        somesite.addParam("form_id", "user_login_form");
        somesite.addParam("mail", "paxbeijing@gmail.com");
        somesite.addParam("pass", "cetas123");
        somesite.addParam("submit", "");
        somesite.addParam("form_token", token);
        config.addLoginConfiguration(somesite);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    /*
     * Instantiate the controller for this crawl.
     */
    PageFetcher pageFetcher = new PageFetcher(config);
    RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
    robotstxtConfig.setEnabled(false);
    RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
    CrawlController controller = new CrawlController(config, pageFetcher, robotstxtServer);

    /*
     * For each crawl, you need to add some seed urls. These are the first
     * URLs that are fetched and then the crawler starts following links
     * which are found in these pages
     */

    controller.addSeed("http://58921.com/alltime?page=60");

    /*
     * Start the crawl. This is a blocking operation, meaning that your code
     * will reach the line after this only when crawling is finished.
     */
    controller.start(LoginCrawler.class, numberOfCrawlers);

    controller.env.close();
}

From source file:niclients.main.regni.java

public static void main(String[] args) throws UnsupportedEncodingException {
    HttpClient client = new DefaultHttpClient();

    if (commandparser(args)) {
        if (createpub()) {
            try {
                HttpResponse response = client.execute(post);
                int resp_code = response.getStatusLine().getStatusCode();
                System.err.println("RESP_CODE: " + Integer.toString(resp_code));
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = rd.readLine()) != null) {
                    System.out.println(line);
                }/*ww  w .j a  va  2s . c om*/

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("Publish creation failed!");
        }

    } else {
        System.err.println("Command line parsing failed!");
    }
}

From source file:org.ado.biblio.desktop.BulkBookLoad.java

public static void main(String[] args) throws IOException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(
            "https://www.kimonolabs.com/api/43h8bb74?apikey=a506e75ca96092b0f73b0ff59c15abe6");
    HttpResponse response = client.execute(request);

    LOGGER.info("Response code [{}]", response.getStatusLine().getStatusCode());

    String responseContent = IOUtils.toString(response.getEntity().getContent());
    final KimonoIsbn kimonoIsbn = new Gson().fromJson(responseContent, KimonoIsbn.class);
    LOGGER.info(kimonoIsbn.toString());/* www  .  j a  v  a  2 s.  c o  m*/

    final List<StringMap> books = kimonoIsbn.getResults().get("books");
    for (StringMap book : books) {
        final String isbnUrl = (String) book.get("isbn");
        LOGGER.info(isbnUrl);
        send(isbnUrl);
    }
}

From source file:yangqi.hc.HttpClientTest.java

/**
 * @param args// www.ja v  a 2  s. co m
 * @throws IOException
 * @throws ClientProtocolException
 */
public static void main(String[] args) throws ClientProtocolException, IOException {
    // TODO Auto-generated method stub
    HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet("http://www.apache.org/");

    // Execute the request
    HttpResponse response = httpclient.execute(httpget);

    // Examine the response status
    System.out.println(response.getStatusLine());

    System.out.println(response.getLocale());

    for (Header header : response.getAllHeaders()) {
        System.out.println(header.toString());
    }

    // Get hold of the response entity
    HttpEntity entity = response.getEntity();

    System.out.println("==========entity=========");
    System.out.println(entity.getContentLength());
    System.out.println(entity.getContentType());
    System.out.println(entity.getContentEncoding());

    // If the response does not enclose an entity, there is no need
    // to worry about connection release
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
            // do something useful with the response
            System.out.println(reader.readLine());

        } catch (IOException ex) {

            // In case of an IOException the connection will be released
            // back to the connection manager automatically
            throw ex;

        } catch (RuntimeException ex) {

            // In case of an unexpected exception you may want to abort
            // the HTTP request in order to shut down the underlying
            // connection and release it back to the connection manager.
            httpget.abort();
            throw ex;

        } finally {

            // Closing the input stream will trigger connection release
            instream.close();

        }

        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

}

From source file:io.alicorn.device.client.DeviceClient.java

public static void main(String[] args) {
    logger.info("Starting Alicorn Client System");

    // Prepare Display Color.
    transform3xWrite(DisplayTools.commandForColor(0, 204, 255));

    // Setup text information.
    //        transform3xWrite(DisplayTools.commandForText("Sup Fam"));

    class StringWrapper {
        public String string = "";
    }//from  w  w  w  .  j ava 2 s .  co  m
    final StringWrapper string = new StringWrapper();

    // Text Handler.
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            String latestString = "";
            String outputStringLine1Complete = "";
            long outputStringLine1Cursor = 1;
            int outputStringLine1Mask = 0;
            String outputStringLine2 = "";

            while (true) {
                if (!latestString.equals(string.string)) {
                    latestString = string.string;
                    String[] latestStrings = latestString.split("::");
                    outputStringLine1Complete = latestStrings[0];
                    outputStringLine1Mask = outputStringLine1Complete.length();
                    outputStringLine1Cursor = 0;

                    // Trim second line to a length of sixteen.
                    outputStringLine2 = latestStrings.length > 1 ? latestStrings[1] : "";
                    if (outputStringLine2.length() > 16) {
                        outputStringLine2 = outputStringLine2.substring(0, 16);
                    }
                }

                StringBuilder outputStringLine1 = new StringBuilder();
                if (outputStringLine1Complete.length() > 0) {
                    long cursor = outputStringLine1Cursor;
                    for (int i = 0; i < 16; i++) {
                        outputStringLine1.append(
                                outputStringLine1Complete.charAt((int) (cursor % outputStringLine1Mask)));
                        cursor += 1;
                    }
                    outputStringLine1Cursor += 1;
                } else {
                    outputStringLine1.append("                ");
                }

                try {
                    transform3xWrite(
                            DisplayTools.commandForText(outputStringLine1.toString() + outputStringLine2));
                    Thread.sleep(400);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
    thread.start();

    // Event Handler
    while (true) {
        try {
            String url = "http://169.254.90.174:9789/api/iot/narwhalText";
            HttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);
            string.string = apacheHttpEntityToString(response.getEntity());

            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}