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);

相关推荐
华仔啊1 天前
千万别给数据库字段加默认值 null!真的会出问题
java·数据库·后端
老赵全栈实战1 天前
【每日一技MyBatis trim标签核心用法
java·mybatis·orm
beata1 天前
Java基础-19:Java 死锁深度解析:从原理、检测到预防与实战指南
java·前端
吾日三省Java2 天前
Spring Cloud架构下的日志追踪:传统MDC vs 王炸SkyWalking
java·后端·架构
爱玩泥巴的小t2 天前
new Thread().start()底层做了什么?
java
码路飞2 天前
GPT-5.4 Computer Use 实战:3 步让 AI 操控浏览器帮你干活 🖥️
java·javascript
Hoffer_2 天前
MySQL 强制索引:USE/FORCE INDEX 用法与避坑
后端·mysql
Hoffer_2 天前
MySQL 索引核心操作:CREATE/DROP/SHOW
后端·mysql
祈安_2 天前
Java实现循环队列、栈实现队列、队列实现栈
java·数据结构·算法
皮皮林5512 天前
拒绝写重复代码,试试这套开源的 SpringBoot 组件,效率翻倍~
java·spring boot