When sending a request to the api SocketTimeoutException: Read timed out
On the server, the software monitors and sends requests to the api from time to time. For about a day, the software works just fine, and then it issues an exception:
java.net.SocketTimeoutException: Read timed out
How do I send a request:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(String.valueOf(message)).build();
response.body().close();
I tried to set different timeouts: 5 seconds, 120 seconds, 12000 seconds.
client.setConnectTimeout(120, TimeUnit.SECONDS); // connect timeout
client.setReadTimeout(120, TimeUnit.SECONDS); // socket timeout
In addition, I did the processing of this exepshna and re-sent the request:
try {
//запрос (и со своими таймаутами, и без)
} catch (SocketTimeoutException e) {
sendRequest(parametres);
}
Also tried just in case just:
throws SocketTimeoutException
Results always the same: work during the day, then exeption. I tried different variations for about a week, but no solution was found.
1 answers
Set the timeout to 0 and see what happens. There are actually 2 options: either everything will be OK, and the requests will pass normally, or they will hang tightly. If the second - it seems to me it is worth reading about how the connection pool works in OkHttp. Perhaps it will be enough to reconfigure something with it.