惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
腾讯CDC
S
SegmentFault 最新的问题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Hacker News
The Hacker News
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
B
Blog
IT之家
IT之家
Spread Privacy
Spread Privacy
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
Cisco Blogs
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
AI
AI
I
InfoQ
H
Heimdal Security Blog
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
W
WeLiveSecurity
SecWiki News
SecWiki News
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
O
OpenAI News
阮一峰的网络日志
阮一峰的网络日志
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
T
Tor Project blog
有赞技术团队
有赞技术团队
Schneier on Security
Schneier on Security
Last Week in AI
Last Week in AI

博客园 - KLAPT

Gateway 网关 CodeX =>Skills Redis 内存满了怎么处理 SpringBoot 默认配置修改 JWT 续签 Access Token + Refresh Token 双 Token claudeCode 命令 MyBatis 的 Mapper 接口 AI | CC GUI 集成 IDEA 完整教程 在IDEA中使用Claude Code IDEA中使用CodeX MyBatisPlus解决大数据量查询慢问题 idea 中的 claude code Token Dubbo 和 Spring Cloud Gateway的区别 Transactional 注解中propagation 掌握 Spring 框架这 10 个扩展点 SpringBoot 快速实现 api 加密 Spring Boot/Cloud 中 bootstrap.yml 与 application.yml SpringBoot 实现 DOCX 转 PDF 微服务Token鉴权设计的几种方案 进程、线程、协程 RSA 加密 Java二维码 ntp服务端和客户端 Chronyd与NTP chronyd 作为服务器时钟 chrony sudo命令和su 的区别 java -cp 和 java -jar Maven 项目打包:实现业务代码与第三方依赖分离 达梦数据库创建用户 梦数据库新增大字段报错问题 达梦数据库操作 MySQL UPDATE多表关联更新 达梦数据库 在Java中调用第三方接口并返回第三方页面 Java调用第三方接口的方法 Nginx 之Rewrite 使用详解 linux 命令 Spring Boot项目中集成Spring Security OAuth2和Apache Shiro
为HTTP POST请求设置请求体
KLAPT · 2025-12-01 · via 博客园 - KLAPT

‌1. 使用 HttpURLConnection

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpPostExample {

public static void main(String[] args) {

try {

URL url = new URL("http://example.com/api");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 设置为POST请求并允许输出

connection.setRequestMethod("POST");

connection.setDoOutput(true);

// 设置请求体数据(例如表单格式)

String data = "key1=value1&key2=value2";

byte[] dataBytes = data.getBytes();

// 可选:设置Content-Type

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

// 写入请求体

OutputStream os = connection.getOutputStream();

os.write(dataBytes); os.flush();

os.close();

// 获取响应

int responseCode = connection.getResponseCode();

System.out.println("Response Code: " + responseCode);

} catch (Exception e) { e.printStackTrace();

} } }

2.使用 Apache HttpClient

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;

public class HttpClientExample {

public static void main(String[] args) {

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

HttpPost post = new HttpPost("http://example.com/api");

// 设置请求头

post.setHeader("Content-Type", "application/json");

// 设置请求体(JSON格式示例)

String jsonBody = "{\"key1\":\"value1\",\"key2\":\"value2\"}";

post.setEntity(new StringEntity(jsonBody));

// 执行请求并获取响应 //

CloseableHttpResponse response = httpClient.execute(post);

// 处理响应...

} catch (Exception e)

{ e.printStackTrace();

} } }