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

相关推荐
JinSoooo5 分钟前
pnpm monorepo 联调:告别 --global 参数
开发语言·javascript·ecmascript·pnpm
孔明兴汉11 分钟前
Cursor MCP 第一章-第一节-MCP协议简介.md
java·ai
信仰_27399324311 分钟前
枚举类Enum反编译后效果
java·开发语言
TDengine (老段)21 分钟前
TDengine 数学函数 LOG 用户手册
java·大数据·数据库·时序数据库·iot·tdengine·涛思数据
青皮桔26 分钟前
Java+OpenCV实现图片切割
java·后端·opencv·计算机视觉
兮动人31 分钟前
Spring中@Configuration注解的proxyBeanMethods属性详解
java·后端·spring
zl97989933 分钟前
SpringBoot-数据访问之Druid
java·spring boot
猫头虎1 小时前
解决升级IDEA2025.2后,每次打开Maven项目爆红的问题:Windows和Mac解决方案
java·ide·macos·maven·intellij-idea·idea·intellij idea
NFG89C1 小时前
Adobe Lightroom Classic 2025解锁版 (专业照片管理)
java·adobe·工具
蒙娜丽宁1 小时前
Rust 性能优化指南:内存管理、并发调优与基准测试案例
开发语言·性能优化·rust