http发送post请求,发送参数为实体

public static String post(JSONObject json, String url) {

String result = "";

HttpPost post = new HttpPost(url);

try {

CloseableHttpClient httpClient = HttpClients.createDefault(); post.setHeader("Content-Type", "application/json;charset=utf-8"); post.addHeader("Authorization", "Basic YWRtaW46");

StringEntity postingString = new StringEntity(json.toString(), "utf-8"); post.setEntity(postingString);

HttpResponse response = httpClient.execute(post);

InputStream in = response.getEntity().getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));

StringBuilder strber = new StringBuilder();

String line = null;

while ((line = br.readLine()) != null) {

strber.append(line + '\n'); }

br.close();

in.close(); result = strber.toString(); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {

result = "服务器异常"; } } catch (Exception e) { System.out.println("请求异常");

throw new RuntimeException(e);

} finally { post.abort(); }

return result; }

EntityDto dto= new EntityDto ();

dto.setUname("哈哈");

dto.setAge(18);

dto.setSalary(12000);

String response = HttpUtil2.post((JSONObject) JSONObject.toJSON(autoTestDto), autoTestUrl);

相关推荐
墨着染霜华7 小时前
Spring Boot整合Kaptcha生成图片验证码:新手避坑指南+实战优化
java·spring boot·后端
DARLING Zero two♡7 小时前
接入 AI Ping 限免接口,让 GLM-4.7 与 MiniMax-M2.1 成为你的免费 C++ 审计专家
开发语言·c++·人工智能
码界奇点7 小时前
Java外功核心7深入源码拆解Spring Bean作用域生命周期与自动装配
java·开发语言·spring·dba·源代码管理
czlczl200209257 小时前
Spring Security @PreAuthorize 与自定义 @ss.hasPermission 权限控制
java·后端·spring
我爱学习好爱好爱7 小时前
Prometheus监控栈 监控java程序springboot
java·spring boot·prometheus
luoluoal7 小时前
基于python的爬虫的贵州菜价可视化系统(源码+文档)
python·mysql·django·毕业设计·源码
老华带你飞7 小时前
考试管理系统|基于java+ vue考试管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
九皇叔叔7 小时前
MySQL 数据库 MVCC 与锁如何联手解决脏读、不可重复读、幻读
数据库·mysql
2501_921649497 小时前
股票 API 对接,接入美国纳斯达克交易所(Nasdaq)实现缠论回测
开发语言·后端·python·websocket·金融
程序喵大人7 小时前
constexpr
开发语言·c++·constexpr