Skip to main content

Java Code Samples

ScreenshotMAX with Java

Use Java's built-in HTTP client (java.net.http) to make signed or direct API calls.

Basic GET Screenshot

import java.io.*;
import java.net.*;

public class Main {
public static void main(String[] args) throws Exception {
String accessKey = "YOUR_ACCESS_KEY";
String api = "https://api.screenshotmax.com/v1/screenshot";
String params = String.format("access_key=%s&url=%s&format=png",
accessKey,
URLEncoder.encode("https://example.com", "UTF-8"));

URL url = new URL(api + "?" + params);
InputStream in = url.openStream();
Files.copy(in, Path.of("screenshot.png"), StandardCopyOption.REPLACE_EXISTING);
in.close();
System.out.println("Image saved to screenshot.png");
}
}

HMAC-Signed GET Screenshot

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.InputStream;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class ScreenshotMAX {
public static void main(String[] args) throws Exception {
String accessKey = "YOUR_ACCESS_KEY";
String secretKey = "YOUR_SECRET_KEY";
String api = "https://api.screenshotmax.com/v1/screenshot";

String urlParam = URLEncoder.encode("https://example.com", "UTF-8");
String format = "png";
String query = "url=" + urlParam + "&format=" + format + "&access_key=" + accessKey;

String signature = sign(secretKey, query);

String finalURL = api + "?" + query + "&signature=" + signature;

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(finalURL))
.GET()
.build();

HttpClient client = HttpClient.newHttpClient();
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());

Files.copy(response.body(), Path.of("screenshot.png"));
System.out.println("Screenshot saved to screenshot.png");
}

private static String sign(String key, String message) throws Exception {
Mac hmac = Mac.getInstance("HmacSHA256");
hmac.init(new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
byte[] hash = hmac.doFinal(message.getBytes(StandardCharsets.UTF_8));

StringBuilder hex = new StringBuilder();
for (byte b : hash) {
hex.append(String.format("%02x", b));
}
return hex.toString();
}
}

Next Steps

  • Explore other API options like HTML to PDF, animated screenshots, or web scraping.
  • Create scheduled recurring screenshots via Scheduled Tasks API

Support

For questions, issues, or feature requests, please contact our support team at support@screenshotmax.com.