41 String imageUrl,
int connectTimeoutSeconds,
int readTimeoutSeconds)
42 throws IOException, InterruptedException {
44 "Attempting to load image from URL: {} (connectTimeout={}s, readTimeout={}s)",
46 connectTimeoutSeconds,
49 HttpClient.newBuilder()
50 .connectTimeout(Duration.ofSeconds(connectTimeoutSeconds))
53 HttpRequest.newBuilder()
54 .uri(URI.create(imageUrl))
55 .timeout(Duration.ofSeconds(readTimeoutSeconds))
56 .header(
"User-Agent",
"Mozilla/5.0")
59 LOG.debug(
"Sending HTTP GET request to {}", imageUrl);
60 HttpResponse<byte[]> response =
61 client.send(request, HttpResponse.BodyHandlers.ofByteArray());
62 LOG.debug(
"Received HTTP response with status code: {}", response.statusCode());
63 if (response.statusCode() >= 200 && response.statusCode() < 300) {
65 "Successfully loaded image from URL: {} ({} bytes)",
67 response.body().length);
68 return response.body();
71 "Failed to load image from URL: {}. HTTP status: {}",
73 response.statusCode());
74 throw new IOException(
"Failed to load image: HTTP " + response.statusCode());