From 21dc010c7c9a85c5fb020993fbc57e8558189539 Mon Sep 17 00:00:00 2001 From: Damian Szczepanik Date: Tue, 14 Apr 2026 21:27:53 +0200 Subject: [PATCH] Fixes SocketException: Resource temporarily unavailable (connect failed) --- java/com/gurock/testrail/APIClient.java | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/java/com/gurock/testrail/APIClient.java b/java/com/gurock/testrail/APIClient.java index 73a75b7..e196c1e 100644 --- a/java/com/gurock/testrail/APIClient.java +++ b/java/com/gurock/testrail/APIClient.java @@ -9,7 +9,7 @@ * * Copyright Gurock Software GmbH. See license.md for details. */ - + package com.gurock.testrail; import java.net.URL; @@ -91,7 +91,7 @@ public void setPassword(String password) * either be an instance of JSONObject or JSONArray (depending on the * API method). In most cases, this returns a JSONObject instance which * is basically the same as java.util.Map. - * + * * If 'get_attachment/:attachment_id', returns a String */ public Object sendGet(String uri, String data) @@ -163,7 +163,7 @@ private Object sendRequest(String method, String uri, Object data) BufferedWriter bodyWriter = new BufferedWriter(new OutputStreamWriter(ostreamBody)); bodyWriter.write("\n\n--" + boundary + "\r\n"); - bodyWriter.write("Content-Disposition: form-data; name=\"attachment\"; filename=\"" + bodyWriter.write("Content-Disposition: form-data; name=\"attachment\"; filename=\"" + uploadFile.getName() + "\""); bodyWriter.write("\r\n\r\n"); bodyWriter.flush(); @@ -218,31 +218,32 @@ private Object sendRequest(String method, String uri, Object data) if (istream == null) { throw new APIException( - "TestRail API return HTTP " + status + + "TestRail API return HTTP " + status + " (No additional error message received)" ); } } - else + else { istream = conn.getInputStream(); } // If 'get_attachment' (not 'get_attachments') returned valid status code, save the file - if ((istream != null) + if ((istream != null) && (uri.startsWith("get_attachment/"))) { FileOutputStream outputStream = new FileOutputStream((String)data); - + int bytesRead = 0; byte[] buffer = new byte[1024]; - while ((bytesRead = istream.read(buffer)) > 0) + while ((bytesRead = istream.read(buffer)) > 0) { outputStream.write(buffer, 0, bytesRead); } - + outputStream.close(); istream.close(); + conn.disconnect(); return (String) data; } @@ -266,6 +267,7 @@ private Object sendRequest(String method, String uri, Object data) } reader.close(); + conn.disconnect(); } Object result; @@ -273,7 +275,7 @@ private Object sendRequest(String method, String uri, Object data) { result = JSONValue.parse(text); } - else + else { result = new JSONObject(); } @@ -304,7 +306,7 @@ private Object sendRequest(String method, String uri, Object data) private static String getAuthorization(String user, String password) { - try + try { return new String(Base64.getEncoder().encode((user + ":" + password).getBytes())); }