#java #timeout #httpurlconnection #socketexception
Вопрос:
Я пытаюсь выполнить HTTP — вызов с Java в службу REST. Все хорошо, пока звонки не займут более 120 секунд. Я могу успешно отправлять запросы в ту же службу, если они занимают менее 120 секунд. Мой код выглядит следующим образом:
URL url = new URL("https://service.myurl.com/my-endpoint");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setReadTimeout(0);
con.setConnectTimeout(0);
con.setDoOutput(true);
con.setDoInput(true);
System.out.println(con.getReadTimeout() "RT");
System.out.println(con.getConnectTimeout() "CT");
System.out.println(System.getProperty("sun.net.client.defaultReadTimeout") "SUN RT");
System.out.println(System.getProperty("sun.net.client.defaultConnectTimeout") "SUN CT");
// System.out.println("Connection expires... " con.getExpiration());
System.out.println("Getting output stream...");
System.out.println(System.getProperty("my-json"));
try (OutputStream os = con.getOutputStream()) {
byte[] input = System.getProperty("my-json").getBytes("utf-8");
os.write(input, 0, input.length);
}
System.out.println("Getting response...");
StringBuilder response = null;
//
// THE REQUEST FAILS IN THE FOLLOWING LINE,
// WHEN TRYING TO RETRIEVE THE INPUT STREAM
// WITH THE RESPONSE FROM THE SERVER
//
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
System.out.println(response.toString());
}
Исключения, которые я получаю, следующие:
javax.net.ssl|WARNING|01|main|2021-03-16 15:49:10.285 UTC|Logger.java:765|handling exception (
"throwable" : {
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:457)
at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1095)
at sun.security.ssl.SSLSocketImpl.access$200(SSLSocketImpl.java:72)
at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:815)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
at com.myurl.test.App.main(App.java:44)}
)
javax.net.ssl|SEVERE|01|main|2021-03-16 15:49:10.286 UTC|Logger.java:765|Fatal (UNEXPECTED_MESSAGE): Connection reset (
"throwable" : {
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:457)
at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1095)
at sun.security.ssl.SSLSocketImpl.access$200(SSLSocketImpl.java:72)
at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:815)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
at com.myurl.test.App.main(App.java:44)}
)
I’ve tried setting the connect and read timeout to 0, and the maximum integer value, as well as the following JVM arguments… -Dsun.net.client.defaultReadTimeout=600000 -Dsun.net.client.defaultConnectTimeout=600000
(10 mins). Nothing has worked for me.
I am pretty sure the problem is with the execution of Java because, on the same server, using CURL I am able to perform that same request which takes longer than 2 minutes.
Any help on this is appreciated.
Спасибо.
РЕДАКТИРОВАТЬ 2021-03-16
Служба может получить запрос и обработать его. Проблема возникает, когда мы пытаемся прочитать ответ от службы.