Customize Device Type with ScraperAPI in Java
Learn how to scrape mobile or desktop versions of websites with ScraperAPI in Java using the device_type parameter. Emulate easily mobile or desktop user agents.
If your use case requires you to exclusively use either desktop or mobile user agents in the headers it sends to the website then you can use the device_type
parameter.
Set
device_type=desktop
to have the API set a desktop (e.g. iOS, Windows, or Linux) user agent. Note: This is the default behavior. Not setting the parameter will have the same effect.Set
device_type=mobile
to have the API set a mobile (e.g. iPhone or Android) user agent.
Note: The device type you set will be overridden if you use keep_headers=true
and send your own user agent in the requests header.
API REQUEST
try {
String apiKey = "APIKEY";
String url = "http://api.scraperapi.com?api_key=" + apiKey + "&url=http://httpbin.org/ip&device_type=mobile";
URL urlForGetRequest = new URL(url);
String readLine = null;
HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
conection.setRequestMethod("GET");
int responseCode = conection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conection.getInputStream()));
StringBuffer response = new StringBuffer();
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
in.close();
System.out.println(response.toString());
} else {
throw new Exception("Error in API Call");
}
} catch (Exception ex) {
ex.printStackTrace();
}
PROXY MODE
try {
String apiKey = "APIKEY";
String proxy = "http://scraperapi.device_type=mobile:" + apiKey + "@proxy-server.scraperapi.com";
URL server = new URL("http://httpbin.org/ip");
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", proxy);
systemProperties.setProperty("http.proxyPort", "8001");
HttpURLConnection httpURLConnection = (HttpURLConnection) server.openConnection();
httpURLConnection.connect();
String readLine = null;
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuffer response = new StringBuffer();
while ((readLine = in.readLine()) != null) {
response.append(readLine);
}
in.close();
System.out.println(response.toString());
} else {
throw new Exception("Error in API Call");
}
} catch (Exception ex) {
ex.printStackTrace();
}
Last updated
Was this helpful?