Java 11: New HTTP client send POST requests with x-www-form-urlencoded parameters
I think the following is the best way to achieve this using Java 11:
Map<String, String> parameters = new HashMap<>();
parameters.put("a", "get_account");
parameters.put("account", account);
String form = parameters.keySet().stream()
.map(key -> key + "=" + URLEncoder.encode(parameters.get(key), StandardCharsets.UTF_8))
.collect(Collectors.joining("&"));
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(this.url))
.headers("Content-Type", "application/x-www-form-urlencoded")
.POST(BodyPublishers.ofString(form)).build();
HttpResponse<?> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.statusCode() + response.body().toString());