Hàm lấy nội dung từ 1 trang web - get content from url

Để lấy nội dung từ 1 trang web bạn dùng hàm dưới đây để gọi nhé

public static String getUrlContents(String theUrl) {
        StringBuilder content = new StringBuilder();
        System.out.println("Call daphoi.com:" + theUrl + "");
        // many of these calls can throw exceptions, so i've just
        // wrapped them all in one try/catch statement.
        try {
            // create a url object
            URL url = new URL(theUrl);

            // create a urlconnection object
            // URLConnection urlConnection = url.openConnection();
            URLConnection urlConnection = url.openConnection();
            urlConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

            // wrap the urlconnection in a bufferedreader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            String line;

            // read from the urlconnection via the bufferedreader
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
              e.printStackTrace();            
        }
        return content.toString();
    }