18

我正在尝试使用新的 http 客户端 api 发送 POST 请求。是否有内置的方式来发送格式为的参数x-www-form-urlencoded

我当前的代码:

HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create(url))
        .header("Content-Type", "application/x-www-form-urlencoded")
        .POST(BodyPublishers.ofString("a=get_account&account=" + URLEncoder.encode(account, "UTF-8")))
        .build();

我正在寻找一种更好的方法来传递参数。像这样的东西:

Params p=new Params();
p.add("a","get_account");
p.add("account",account);

我需要自己构建这个功能还是已经内置了?

我正在使用 Java 12。

4

5 回答 5

10

我认为以下是使用 Java 11 实现此目的的最佳方法:

Map<String, String> parameters = new HashMap<>();
parameters.put("a", "get_account");
parameters.put("account", account);

String form = parameters.entrySet()
    .stream()
    .map(e -> e.getKey() + "=" + URLEncoder.encode(e.getValue(), StandardCharsets.UTF_8))
    .collect(Collectors.joining("&"));

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create(url))
    .headers("Content-Type", "application/x-www-form-urlencoded")
    .POST(HttpRequest.BodyPublishers.ofString(form))
    .build();

HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.ofString());

System.out.println(response.statusCode() + " " + response.body().toString());
于 2020-06-23T12:03:28.110 回答
2

这种方式可能很有用:

String param = Map.of("param1", "value1", "param2", "value2")
      .entrySet()
      .stream()
      .map(entry -> Stream.of(
               URLEncoder.encode(entry.getKey(), UTF_8),
               URLEncoder.encode(entry.getValue(), UTF_8))
                .collect(Collectors.joining("="))
      ).collect(Collectors.joining("&"));

您最多可以使用 10 对 (param, value) by Map.of(...)。它返回一个不可修改的地图。

于 2020-06-23T12:37:51.537 回答
2

正如 Łukasz Olszewski 所说,工作正常:

String params = Map.of(
                    Constants.PARAM_CLIENT_ID, apiObject.getClientId(),
                    Constants.PARAM_SCOPE, apiObject.getScope(),
                    Constants.PARAM_CODE, apiObject.getCode(),
                    Constants.PARAM_REDIRECT_URI, apiObject.getRedirectUri(),
                    Constants.PARAM_GRANT_TYPE, apiObject.getGrantType(),
                    Constants.PARAM_CODE_VERIFIER, apiObject.getCodeVerifier())
                    .entrySet()
                    .stream()
                    .map(entry -> Stream.of(
                            URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8),
                            URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8))
                            .collect(Collectors.joining("="))
                    ).collect(Collectors.joining("&"));

HttpResponse<?> response = utils.consumeHttpPostFormUrlEncodedClientByRequestUrl(Constants.URL_BASE + Constants.URL_GET_TOKEN, params);

并使用HttpPostFormUrlEncodedClientByRequestUrl

public HttpResponse<?> consumeHttpPostFormUrlEncodedClientByRequestUrl(String url, String map) throws IOException, InterruptedException {
        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
                .header("Content-Type", String.valueOf(MediaType.APPLICATION_FORM_URLENCODED))
                .POST(HttpRequest.BodyPublishers.ofString(map))
                .build();
        return httpClient.send(request, HttpResponse.BodyHandlers.ofString());
    }
于 2021-01-24T03:32:37.263 回答
1

检查甲醇。它对身体有好处FormBodyPublisherx-www-form-urlencoded

var formBody = FormBodyPublisher.newBuilder()
      .query("a", "get_account")
      .query("account", account)
      .build();
var request = MutableRequest.POST("https://example.com", formBody);

// Methanol implements an HttpClient that does nice things to your request/response.
// Here, the Content-Type header will be added for you.
var client = Methanol.create();
var response = client.send(request, BodyHandlers.ofString());
于 2021-02-10T13:59:24.607 回答
1

而不是 Stream.of 您可以使用更紧凑的 String.join (根据 Łukasz Olszewski 的回答):

String form = Map.of("param1", "value1", "param2", "value2")
  .entrySet()
  .stream()
  .map(entry -> String.join("=",
                        URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8),
                        URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8)))
                .collect(Collectors.joining("&"));
return HttpRequest.BodyPublishers.ofString(form);
于 2021-09-08T07:25:30.630 回答