Langsame Antwort auf die HTTPS -Anforderung [Duplikat]Java

Java-Forum
Anonymous
 Langsame Antwort auf die HTTPS -Anforderung [Duplikat]

Post by Anonymous »

Als wir HTTP in HTTPS verlegt haben, beobachten wir, dass Streaming-Datenantwort nach einer Verzögerung erfolgt. So reparieren Sie von meiner Seite mithilfe der Java-HTTP-Verbindung.

Code: Select all

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();
}
}
Bitte schlagen Sie vor, wenn ich meinen Code verbessern kann

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post