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 分钟前
Mybatis二级缓存实现详解
java·数据库·后端·架构·mybatis
源码获取_wx:Fegn08954 分钟前
基于springboot + vue健康茶饮销售管理系统
java·vue.js·spring boot·后端·spring
小帅学编程9 分钟前
Spring(侧重注解开发)
java·学习·spring
早点睡觉好了16 分钟前
JAVA中基本类型和包装类型的区别
java·开发语言
阿拉伯柠檬18 分钟前
MySQL内置函数
linux·数据库·mysql·面试
雅俗共赏zyyyyyy19 分钟前
SpringBoot集成配置文件加解密
java·spring boot·后端
计算机学姐27 分钟前
基于SpringBoot的送货上门系统【2026最新】
java·vue.js·spring boot·后端·mysql·spring·tomcat
码农水水30 分钟前
国家电网Java面试被问:二叉树的前序、中序、后序遍历
java·开发语言·面试
Yana.nice33 分钟前
JMS与JDBC
java
Respect@34 分钟前
qml之TableViewColumn
开发语言·qml