How to handle this error: java.io.IOException: Server returned HTTP response code: 503 for
I wish that when gave this error, my code would repeat the request of url.
No is none this errors:
1) The server may be busy
2) The server may be down for maintenance
Code:
public Element trataDados(String address1) throws MalformedURLException, IOException, JDOMException {
URL url;
url = new URL(address1);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
InputStreamReader reader = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader bfreader = new BufferedReader(reader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bfreader.readLine()) != null) {
stringBuilder.append(line);
}
bfreader.close();
conn.disconnect();
Get the response code before you start reading the input. If it's 503, don't read the input, do read the error stream, and close it, and then retry, but not too soon. If the server is down for maintenance it could be for five seconds or five days. You should probably use an exponentially-increasing retry interval like TCP does, say doubling it every time, or multiplying by 1.5, up to some limit where you just give up.
NB GET
is already the default.