简体   繁体   中英

how to deal with 503 response code java.net

Would the following be an appropriate way of dealing with a 503 response code in java networking? What does this code- specifically the calls to disconnect and null do?

URL url = new URL(some url);
HttpURLConnection h =(HttpURLConnection)url.openConnection();
int x = h.getResponseCode();
while(x==503)
{
    h.disconnect();
    h = null;
    h =(HttpURLConnection)url.openConnection();
    x = h.getResponseCode();
}

The disconnect() closes the underlying TCP socket.

Setting the local variable to null immediately before reassigning it accomplishes nothing whatsoever.

There should be a sleep in that loop, with an interval that increases on every failure, and a limited number of retries.

Whatever you want it to do is an appropriate way. To make something failsafe, it would be better to repeat until success is achieved, rather than only handling a 503 scenario.

simplest example: loop until 200 (success) code comes back.

(better would be to abstract that out into methods and classes and use OOP and unit tests where possible.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM