When we moved http to https then we observed streaming data response come after some delay so how to fix from my side using java http connection.
Here my https request code
HttpURLConnection connection = null;try { // Create URL object URL obj = new URL(url); connection = (HttpURLConnection) obj.openConnection(); // Set the HTTP method to POST connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); // Enable input/output streams connection.setDoOutput(true); // Send the POST data try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonData.getBytes(StandardCharsets.UTF_8); os.write(input, 0, input.length); // write the data to the server } try { InputStream inputStream = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); BufferedReader reader = new BufferedReader(inputStreamReader); String line; while ((line = reader.readLine()) != null) { if (line.startsWith("data: ")) { line = line.substring(6).trim(); } if (line.contains("content")) { try { ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(line); String value = jsonNode.get("content").asText(); responseCodeOrResultTextArea.append(value); } catch (Exception e) { throw new RuntimeException(e); } } } reader.close(); inputStream.close(); connection.disconnect(); } catch (IOException e) { System.err.println("Error reading stream: " + e); }} catch (SocketTimeoutException ex) { JOptionPane.showMessageDialog( mainPanel, "Request timed out: " + ex.getMessage());} catch (IOException ex) { JOptionPane.showMessageDialog( mainPanel, "Connection failed: " + ex.getMessage());} finally { // Ensure the connection is closed if (connection != null) { connection.disconnect(); }}
please suggest if i can improve my code