diff --git a/project/src/project/utils/HttpUtils.java b/project/src/project/utils/HttpUtils.java new file mode 100644 index 0000000..babcc52 --- /dev/null +++ b/project/src/project/utils/HttpUtils.java @@ -0,0 +1,30 @@ +package project.utils; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class HttpUtils { + public static String getHtml(String url) throws Exception { + URL obj = new URL(url); + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("User-Agent", "Mozilla/5.0"); + + int responseCode = con.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_OK) { + throw new Exception("HTTP error code: " + responseCode); + } + + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); + String inputLine; + StringBuilder html = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + html.append(inputLine); + } + in.close(); + return html.toString(); + } +} \ No newline at end of file