You can do this by manually checking the status code, something like this:
CloseableHttpResponse response = null; boolean success = false; while(!success) { response = client.execute(httpGet); int status = response.getStatusLine().getStatusCode(); success = (status == 200); if (!success) { if(status == 403) { Thread.sleep(2000); // wait 2 seconds before retrying } else { throw new RuntimeException("Something went wrong: HTTP status: " + status); } } } String contents = EntityUtils.toString(response.getEntity()); response.close(); // .... System.out.println(contents);
You need to add some things, for example, repeat the set number of times before throwing the final exception and catch some checked exceptions (for example, the InterruptedException created by Thread.sleep() ), but basically the code shows the main idea.
morgano
source share