Making POST/PUT Requests

Some advanced users may want to send POST/PUT requests in order to scrape forms and API endpoints directly. You can do this by sending a POST/PUT request through ScraperAPI. The return value will be stringified. So if you want to use it as JSON, you will need to parse it into a JSON object.

  • API ENDPOINT REQUEST

try {
String apiKey = "YOURAPIKEY";
String url = "http://api.scraperapi.com?api_key=" + apiKey + "&url=http://httpbin.org/ip";
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:" + 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