java通用post请求
2022/7/17 1:17:45
本文主要是介绍java通用post请求,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.example.climbnumber.yzm; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; 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.util.EntityUtils; public class Test04 { public static void main(String[] args) { JSONObject jsonObject = JSONObject.parseObject("{\n" + " \"body\": {\n" + " \"uuid\": \"\",\n" + " \"scenes\": \"xxxx(91522301MA6DJJYG2C)\"\n" + " },\n" + " \"head\": {\n" + " \"transCode\": \"\",\n" + " \"traceId\": \"\"\n" + " }\n" + "}"); JSONObject result = sendPost("www.baidu.com",jsonObject); System.out.println("================================"); System.out.println(result.toJSONString()); } public static JSONObject sendPost(String url,JSONObject param){ //定义接收数据 JSONObject result = new JSONObject(); HttpPost httpPost = new HttpPost(url); CloseableHttpClient client = HttpClients.createDefault(); //请求参数转JOSN字符串 StringEntity entity = new StringEntity(param.toJSONString(), "UTF-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); try { HttpResponse response = client.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8")); } } catch (Exception e) { e.printStackTrace(); result.put("error", "连接错误!"); } return result; } }
这篇关于java通用post请求的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-20测试人员都是画画大神,让我看看谁还不会用代码图?
- 2024-05-20年薪百万的程序员都在用的摸鱼方式……
- 2024-05-19永别了,微服务架构!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了