-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlFetcher.java
More file actions
32 lines (29 loc) · 1.12 KB
/
HtmlFetcher.java
File metadata and controls
32 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.*;
import java.net.*;
import java.util.*;
public class HtmlFetcher {
/**
* Performs an HTTP GET request and returns the HTML as a list of lines.
* @param urlString URL of the website to be analyzed.
* @return List of HTML lines.
* @throws IOException If there is an error connecting to the URL.
*/
public List<String> fetchHtml(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
List<String> htmlLines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
htmlLines.add(line);
}
}
} finally {
connection.disconnect(); // Closes the connection when finished
}
return htmlLines;
}
}