Our Java Script solution now gives you the ability to take a screenshot of the target page through the use of the parameter screenshot=true
. This parameter automatically enables JS rendering to get the full page content, before taking a screenshot.
Copy try {
String apiKey = "API_KEY";
String url = "https://api.scraperapi.com?api_key=" + apiKey + "&screenshot=true&url=https://example.com/";
URL urlForGetRequest = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlForGetRequest.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
System.out.println("HTTP Response Code: " + responseCode);
System.out.println("\nResponse Headers:");
Map<String, List<String>> headers = connection.getHeaderFields();
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
System.out.println(header.getKey() + ": " + String.join(", ", header.getValue()));
}
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer response = new StringBuffer();
String readLine;
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
in.close();
System.out.println("\nResponse Body:");
System.out.println(response.toString());
} else {
System.out.println("Error: HTTP Response Code " + responseCode);
throw new Exception("Error in API Call");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Copy try {
URL asyncApiUrl = new URL("https://async.scraperapi.com/jobs");
HttpURLConnection connection = (HttpURLConnection) asyncApiUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
String jsonPayload = "{"
+ "\"apiKey\": \"API_KEY\","
+ "\"url\": \"https://example.com\","
+ "\"apiParams\": {"
+ "\"screenshot\": \"true\""
+ "}"
+ "}";
try (OutputStream outputStream = connection.getOutputStream()) {
outputStream.write(jsonPayload.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
}
Get response code
int responseCode = connection.getResponseCode();
System.out.println("HTTP Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String readLine;
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
System.out.println("Response from server: " + response.toString());
}
} else {
System.out.println("HTTP Error Code: " + responseCode);
throw new Exception("Error in API Call");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}