【发布时间】:2020-09-30 23:28:56
【问题描述】:
我正在编写一个 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();
【问题讨论】:
标签: java rest httpurlconnection