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();
}
相关推荐
tritone20 小时前
在优豆云免费云服务器上搭建与配置Apache的实践笔记
服务器·网络·apache·1024程序员节
RTC老炮20 小时前
webrtc弱网-PccNetworkController类源码分析与算法原理
网络·算法·webrtc
胡耀超21 小时前
5、服务器互连技术(小白入门版)
服务器·网络·ai·网络拓扑·gpu·pcie·1024程序员节
wanhengidc21 小时前
服务器硬件设备都有哪些?
运维·服务器·网络·游戏·智能手机·架构·云计算
云卓SKYDROID21 小时前
飞控开发难点与技术要点
网络·智能路由器·无人机·高科技·云卓科技
专家大圣1 天前
Docker+Redis监控新方案:cpolar让远程管理“零配置”
网络·redis·docker·容器·内网穿透
Xの哲學1 天前
Linux NAPI 架构详解
linux·网络·算法·架构·边缘计算
造价女工1 天前
视频监控系统原理与计量
网络·音视频·状态模式·消防·工程造价
wacpguo1 天前
centos 配置网络
linux·网络·centos
子燕若水1 天前
TLS/SSL加密通信过程全解
网络·网络协议·ssl