Check Website for Text help

Discussion in 'Spigot Plugin Development' started by JackPlayzMC, Dec 30, 2016.

  1. Hello there. So I'm fairly new to this and I want a way for my plugin to check a URL link for Text, for example "false". And if the website returns the word "false" the plugin will then do something. So can someone give me an example? And explain how it works too, please. Thanks!
     
  2. Using either Java's built in HttpURLConnection or the Apache httpclient library, you can make a GET request out to the specific URL. The response of that will just be whatever you were fetching, if it's a text file with the word 'false' in it, it'll just be "false". If it's an html page, it'll return that.

    This is an example with Apache's httpclient
    Code (Text):

    public String getTextFromUrl(URL url) {
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet req = new HttpGet(url);
       
        HttpResponse res = client.execute(req);
        System.out.println("Response code: " + response.getStatusLine().getStatusCode());

        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent());
        StringBuilder result = new StringBuilder();
        String line;

        while((line = in.readLine()) != null) {
            result.append(line);
        }

        return result.toString();
    }
     
  3. @JackPlaysMC
    And in case it is a more complex web page (e.g. a normal website as this one), you can use things like JSoup to parse it. Dynamic content (JS and stuff) won't work, but you rarely need this.