public static JSONObject post(String urlString, ParamInfo paramInfo) throws Exception {
URL url = new URL(urlString);
JSONObject jsonObject = new JSONObject();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // 设置该连接可输入
conn.setDoOutput(true); // 设置该连接可输出
conn.setRequestMethod("POST"); // 设置请求方式
conn.setRequestProperty("accept","application/json");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
String paramJsonStr = JSON.toJSONString(paramInfo);
// 注释掉的方法没成功
// conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
// System.out.println(paramJsonStr);
HttpURLConnection post请求 传参数
最新推荐文章于 2024-11-16 10:46:39 发布
本文详细介绍了如何利用Java的HttpURLConnection类进行POST请求,包括设置请求方法、添加请求头和参数,以及发送和接收数据的过程。

最低0.47元/天 解锁文章
957

被折叠的 条评论
为什么被折叠?



