-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlFetcher.java
More file actions
37 lines (33 loc) · 1.14 KB
/
Copy pathHtmlFetcher.java
File metadata and controls
37 lines (33 loc) · 1.14 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
33
34
35
36
37
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Classe responsável por obter o conteúdo HTML de uma URL.
*/
public class HtmlFetcher {
/**
* Busca o conteúdo HTML de uma URL.
* @param urlString URL a ser acessada.
* @return Conteúdo HTML como String.
* @throws UrlConnectionException Se a conexão falhar.
*/
public String fetchContent(String urlString) throws UrlConnectionException {
StringBuilder content = new StringBuilder();
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
}
} catch (Exception e) {
throw new UrlConnectionException("URL connection error", e);
}
return content.toString();
}
}