Adding Parameters to Apache HttpClient Requests – 向Apache HttpClient请求添加参数

最后修改: 2020年 12月 8日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

HttpClient is part of the Apache HttpComponents project that provides a toolset of low-level Java components focused on HTTP and associated protocols. The most essential function of HttpClient is to execute HTTP methods.

HttpClient是Apache HttpComponents项目的一部分,它提供了一个专注于HTTP和相关协议的低级Java组件的工具集。HttpClient的最基本功能是执行HTTP方法。

In this short tutorial, we’ll discuss adding parameters to HttpClient requests. We’ll learn how to use UriBuilder with String name-value pairs and also NameValuePairs. Similarly, we’ll see how to pass parameters using UrlEncodedFormEntity.

在这个简短的教程中,我们将讨论向HttpClient请求添加参数。我们将学习如何使用UriBuilder与字符串名-值对以及NameValuePairs。同样地,我们将看到如何使用UrlEncodedFormEntity传递参数。

2. Add Parameters to HttpClient Requests Using UriBuilder

2.使用UriBuilderHttpClient请求添加参数

UriBuilder helps us to easily create URIs and add parameters via builder pattern. We can add parameters using String name-value pairs, or utilize NameValuePairs class for that purpose.

UriBuilder帮助我们轻松创建URI并通过构建器模式添加参数。我们可以使用String名-值对来添加参数,或者利用NameValuePairs类来实现这一目的

In this example, a final URL should look like this:

在这个例子中,最终的URL应该是这样的。

https://example.com?param1=value1&param2=value2

Let’s see how to use String name-value pairs:

让我们看看如何使用String名-值对。

public CloseableHttpResponse sendHttpRequest() {
    HttpGet httpGet = new HttpGet("https://example.com");
    URI uri = new URIBuilder(httpGet.getURI())
      .addParameter("param1", "value1")
      .addParameter("param2", "value2")
      .build();
   ((HttpRequestBase) httpGet).setURI(uri);
    CloseableHttpResponse response = client.execute(httpGet);
    client.close();
}

Also, we can go with the NameValuePair list for HttpClient request:

另外,我们可以用NameValuePair列表来处理HttpClient请求。

public CloseableHttpResponse sendHttpRequest() {
    List nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
    nameValuePairs.add(new BasicNameValuePair("param2", "value2"));
    HttpGet httpGet = new HttpGet("https://example.com");
    URI uri = new URIBuilder(httpGet.getURI())
      .addParameters(nameValuePairs)
      .build();
   ((HttpRequestBase) httpGet).setURI(uri);
    CloseableHttpResponse response = client.execute(httpGet);
    client.close();
}

Similarly, UriBuilder can be used to add parameters to other HttpClient request methods.

类似地,UriBuilder可以被用来向其他HttpClient请求方法添加参数。

3. Add Parameters to HttpClient Request Using UrlEncodedFormEntity

3.使用UrlEncodedFormEntityHttpClient请求添加参数

Another approach would be to utilize UrlEncodedFormEntity:

另一种方法是利用UrlEncodedFormEntity

public CloseableHttpResponse sendHttpRequest() {
    List nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
    nameValuePairs.add(new BasicNameValuePair("param2", "value2"));
    HttpPost httpPost = new HttpPost("https://example.com");
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
    CloseableHttpResponse response = client.execute(httpPost);
    client.close();
}

Notice that UrlEncodedFormEntity couldn’t be used for GET requests, since GET request does not have a body that could contain an entity.

请注意,UrlEncodedFormEntity不能用于GET请求,因为GET请求没有一个可能包含实体的主体。

4. Conclusion

4.总结

In this example, we showed how to add parameters to HttpClient requests. Also, the implementation of all these examples and code snippets are available over on GitHub.

在这个例子中,我们展示了如何向HttpClient请求添加参数。另外,所有这些例子的实现和代码片段都可以在GitHub上找到。