Skip to content

Instantly share code, notes, and snippets.

@flavienlaurent
Last active January 2, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavienlaurent/8350081 to your computer and use it in GitHub Desktop.
Save flavienlaurent/8350081 to your computer and use it in GitHub Desktop.
How to retry request with http-request lib
public interface HttpRequestBuilder {
public HttpRequest build();
}
HttpRequestBuilder builder = new HttpRequestBuilder {
@Override
public HttpRequest build() {
return HttpRequest.get("uri").acceptJson().userAgent("useragent");
}
}
//to retry on SocketTimeoutException for example
HttpRequest httpRequest = builder.build();
try {
boolean ok = httpRequest.ok();
//...
} catch(HttpRequest.HttpRequestException e) {
if(e.getCause() instanceof SocketTimeoutException) {
//repeat it
httpRequest = builder.build();
httpRequest.code();
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment