向端点发送 Post 请求但忽略响应

Send Post request to endpoint but ignore response

我正在编写一个 java 方法来向我们的一个端点发出 post 请求。 所有方法需要担心的是发送请求,但不关心端点是否收到它(即发即弃)。

如何使用 HttpUrlConnection 实现此目的?

我有以下信息,但我不确定是否触发 connection.disconnect();是最佳做法吗?

    HttpURLConnection connection = (HttpURLConnection) EndpointUrl.openConnection();
    // Set the http rest call to POST.
    connection.setRequestMethod("POST");
    // Set the request properties, including the Authorization tag.
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Authorization", header);
    connection.setDoOutput(true);

    ObjectMapper mapper = new ObjectMapper();
    try(OutputStream outputStream = connection.getOutputStream()) {
        byte[] input = mapper.writeValueAsBytes(log);
        outputStream.write(input, 0, input.length);           
    }
    connection.disconnect();

来自docs.oracle.com

disconnect()

Indicates that other requests to the server are unlikely in the near future.

为了简单地向客户端发送消息然后断开连接,我认为这是最安全的做法。请注意,调用 disconnect() 不应意味着 HttpURLConnection 实例可以重新用于新连接。