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

相关推荐
星释4 小时前
Rust 练习册 :Luhn与校验算法
java·算法·rust
代码雕刻家4 小时前
C语言中关于类型转换不匹配的解决方案
c语言·开发语言·算法
RedJACK~4 小时前
Go Ebiten小游戏开发:扫雷
开发语言·后端·golang
深圳市恒讯科技4 小时前
如何在服务器上安装和配置数据库(如MySQL)?
服务器·数据库·mysql
程序猿_极客5 小时前
【2025】16届蓝桥杯 Java 组全题详解(省赛真题 + 思路 + 代码)
java·开发语言·职场和发展·蓝桥杯
毕设源码-邱学长5 小时前
【开题答辩全过程】以 “万家电器”仓库管理系统的设计与实现为例,包含答辩的问题和答案
java·eclipse
摇滚侠5 小时前
Spring Boot3零基础教程,响应式编程的模型,笔记109
java·spring boot·笔记
wfsm5 小时前
flowable使用01
java·前端·servlet
员大头硬花生5 小时前
七、InnoDB引擎-架构-后台线程
java·数据库·mysql
玉树临风江流儿5 小时前
C++左值、右值、move移动函数
开发语言·c++