-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebScraper.java
More file actions
31 lines (25 loc) · 1.09 KB
/
Copy pathWebScraper.java
File metadata and controls
31 lines (25 loc) · 1.09 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
package com.stacktips.threads;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class WebScraper {
String scrapeWebsite(String urlString) {
try (HttpClient client = HttpClient.newHttpClient()) {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(urlString))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return String.format("URL: %s, Content Length: %d bytes", urlString, response.body().length());
} catch (URISyntaxException e) {
System.out.println("URI Syntax Exception: " + e.getMessage());
} catch (IOException e) {
System.out.println("URI Syntax Exception: " + e.getMessage());
} catch (InterruptedException e) {
System.out.println("URI Syntax Exception: " + e.getMessage());
}
return "Error: Unable to scrape website.";
}
}