The function is very simple:
public static String readURL(URL url, HTTPMethod method) throws IOException {
FetchOptions opt = FetchOptions.Builder.doNotValidateCertificate();
HTTPRequest request = new HTTPRequest (url, method, opt);
URLFetchService service = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response = service.fetch(request);
byte[] content = response.getContent();
return new String(content);
}
The problem is that occasionally I get an exception:
java.lang.RuntimeException: java.io.IOException: Timeout while fetching: https://graph.facebook.com...
What I would like to do is to the function in some while() loop or similar so it keeps trying until the response is there. What do you think would be the best approach? Am I heading to the right direction or would you suggest something totally different? Increasing the timeout to 10 secs as was suggested in some other posts should avoid the majority of the issues, but not eradicate the problem.
Thanks.
PS 1:
The line FetchOptions opt = FetchOptions.Builder.doNotValidateCertificate();
is needed to avoid another problem,
HTTP ERROR 500
Problem accessing /auth. Reason:
javax.net.ssl.SSLHandshakeException: Could not verify SSL certificate for: https://graph.facebook.com/oauth/access_token?...
PS 2: This is not an issue like in this thread: GoogleAppEngine urlfetch timeout exception because I'm fetching the facebook servers, not my own server. Nor is like "Timeout while fetching" URLFetch GAE/J because the issue is not a very large feed, but an non-responsive server (specially slow when responding requests by test users)