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();
}
相关推荐
九州ip动态3 小时前
手机设备多?怎样设置IP保证不关联
网络协议·tcp/ip·智能手机
hgdlip3 小时前
换ip是换网络的意思吗?怎么换ip地址
服务器·网络·tcp/ip
mooyuan天天6 小时前
pikachu靶场通关笔记05 XSS关卡01-反射型GET
网络·web安全·反射型xss·pikachu靶场·xss漏洞
智联视频超融合平台7 小时前
无人机+AI视频联网:精准狙击,让‘罪恶之花’无处藏身
人工智能·网络协议·安全·系统安全·音视频·无人机
I won.7 小时前
计算机网络 HTTP篇常见面试题总结
网络协议·计算机网络·http
星鑫会IP8 小时前
动态IP与区块链:重构网络信任的底层革命
网络·tcp/ip·区块链
斯普信云原生组9 小时前
K8S集群主机网络端口不通问题排查
网络·容器·kubernetes
浩浩测试一下9 小时前
reverse_ssh 建立反向 SSH 连接指南 混淆&&AV [好东西哟]
运维·开发语言·网络·安全·网络安全·ssh·php
巴拉特好队友10 小时前
用wireshark抓了个TCP通讯的包
网络·tcp/ip·wireshark
2401_8368365910 小时前
LVS-DR群集
服务器·网络·lvs