Reputation: 133
I need to check if the response code of a URL is 200 or not. While processing an array of URLs in a loop, I'm getting 200 for some of them and 503 for others. Even though all the URLs are valid and return 200 when run without a loop.
for (int i = 0; i < 10; i++) {
URL url = new URL("https://www.amazon.in/dp/B01N6TDY3Y");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int status = urlConnection.getResponseCode();
System.out.println(status);
}
This code prints different status each time. This time it was:
200
200
200
200
200
200
503
200
503
200.
Even though the url is same each time. Any idea why?
Upvotes: 1
Views: 396
Reputation: 719261
Often times, a high availability service will be implemented a number of web servers sitting behind a load balancer. Requests sent to the service could go to a different server each time. If one is not working properly, you could see different responses for the same request sent repeatedly.
Another factor is that you may be hitting a server that is restarting, or that is temporarily overloaded. That is what a 503 is intended to indicate.
Upvotes: 0
Reputation: 1680
The response code is not always the same for a given url. It depends of what happened when processing the request or what the designer of the application decided.
200 means everithing went ok 503 means a problem occured maybe thred pool exhausted and request could not be processed.
Usually, when you request an API or endpoint you should know what codes the server will return to your calls prior to develop your code.
Check this Http codes
Upvotes: 1