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();
}
相关推荐
moonless02221 天前
FastAPI框架,这一小篇就能搞懂精髓。
http·fastapi
白帽黑客沐瑶2 天前
【网络安全就业】信息安全专业的就业前景(非常详细)零基础入门到精通,收藏这篇就够了
网络·安全·web安全·计算机·程序员·编程·网络安全就业
树码小子2 天前
Java网络编程:(socket API编程:TCP协议的 socket API -- 回显程序的服务器端程序的编写)
java·网络·tcp/ip
绿箭柠檬茶2 天前
Ubuntu 服务器配置转发网络访问
服务器·网络·ubuntu
FPGA_Linuxer2 天前
FPGA 40 DAC线缆和光模块带光纤实现40G UDP差异
网络协议·fpga开发·udp
real 12 天前
传输层协议UDP
网络·网络协议·udp
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
喵手2 天前
玩转Java网络编程:基于Socket的服务器和客户端开发!
java·服务器·网络
徐子元竟然被占了!!2 天前
实验-基本ACL
网络
ftpeak2 天前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app