HttpPost 类(构建 HTTP POST 请求)

HttpPost 类是 Apache HttpClient 库中的一个类,用于构建 HTTP POST 请求。以下是 HttpPost 类的一些常用方法和代码案例:

常用方法

  1. 构造方法

    • HttpPost(String uri):创建一个 HttpPost 对象,并将请求的 URI 作为参数传递。
  2. 设置请求体

    • setEntity(HttpEntity entity):设置 POST 请求的请求体。
  3. 添加请求头

    • addHeader(String name, String value):向请求中添加一个头信息。
    • setHeaders(Header[] headers):设置请求的所有头信息。
  4. 配置请求参数

    • setConfig(RequestConfig config):设置请求的配置,如超时时间等。

代码案例

案例 1 :使用 HttpPost 发送一个简单的 POST 请求。

java 复制代码
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://example.com/api");

try {
    HttpResponse response = httpClient.execute(httpPost);
    String responseString = EntityUtils.toString(response.getEntity());
    System.out.println(responseString);
} finally {
    httpClient.close();
}

在这个例子中,我们创建了一个 HttpPost 对象,并使用 httpClient 执行了请求。响应内容被转换成字符串并打印出来。

案例 2 :使用 HttpPost 发送一个包含 JSON 数据的 POST 请求。

java 复制代码
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

String json = "{\"key\":\"value\"}";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://example.com/api");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(json, "UTF-8"));

try {
    HttpResponse response = httpClient.execute(httpPost);
    String responseString = EntityUtils.toString(response.getEntity());
    System.out.println(responseString);
} finally {
    httpClient.close();
}

在这个例子中,我们创建了一个包含 JSON 数据的 StringEntity,并将其设置为 HttpPost 对象的请求体。我们还设置了内容类型为 application/json

案例 3 :使用 HttpPost 发送一个包含表单数据的 POST 请求。

java 复制代码
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://example.com/api");

MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("field1", new StringBody("value1"));
builder.addPart("field2", new StringBody("value2"));

HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);

try {
    HttpResponse response = httpClient.execute(httpPost);
    String responseString = EntityUtils.toString(response.getEntity());
    System.out.println(responseString);
} finally {
    httpClient.close();
}
相关推荐
HansenPole8254 分钟前
元编程笔记
笔记·网络协议·rpc
TG:@yunlaoda360 云老大37 分钟前
华为云国际站代理商TaurusDB的读写分离可以应用于哪些场景?
服务器·网络·数据库·华为云
ICT技术最前线41 分钟前
深信服交换机配置命令教程
网络·交换机·深信服·交换机配置教程
星哥说事1 小时前
SSL/TLS 证书管理,文件与数据库加密技术
数据库·网络协议·ssl
不知道累,只知道类1 小时前
[故障复盘] 生产环境 HTTP 连接池耗尽导致的“服务假死”分析
网络·网络协议·http
誰能久伴不乏1 小时前
Linux `epoll` 学习笔记:从原理到正确写法(含 ET 经典坑总结)
linux·服务器·网络·c++·ubuntu
init_23611 小时前
MPLS option ABC区别
网络
自由生长20242 小时前
计算机网络-从CGI 到 Unix Domain Socket:理解 Web 服务背后的进程通信演进
网络协议
weixin_445476682 小时前
Docker 在 Ubuntu(国内网络)安装及问题解决总结
网络·ubuntu·docker
雍凉明月夜2 小时前
深度学习网络笔记Ⅰ(CNN)
网络·笔记·深度学习·神经网络·学习·cnn