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

相关推荐
下地种菜小叶几秒前
特征定义、特征计算、特征服务怎么配合?一次讲透
java·服务器·前端·数据库·spring cloud
报错小能手3 分钟前
Swift UI 用 MVVM 架构 Combine EventBus 实现待办清单
开发语言·ui·swift
威迪斯特5 分钟前
Cobra框架:Go语言命令行开发的现代化利器
开发语言·前端·后端·golang·cobra·交互模型·命令行框架
lifewange7 分钟前
Idea如何调大字体
java·macos·intellij-idea
许彰午11 分钟前
# 一个Java老鸟的TensorFlow入门——从计算图到GradientTape
java·tensorflow·neo4j
itzixiao12 分钟前
L1-055 谁是赢家(10 分)[java][python]
java·python·算法
IT利刃出鞘12 分钟前
Java反射--PropertyDescriptor的使用
java·开发语言
㳺三才人子15 分钟前
容器內的 H2 控制台
开发语言·前端·javascript
Evand J15 分钟前
【MATLAB程序】基于RSSI的RFID二维轨迹定位仿真介绍,EKF滤波增加轨迹定位精度。附下载链接
开发语言·matlab·平面·滤波·定位·导航
遇见火星16 分钟前
Firewalld 防火墙实战指南 + TCPWrapper 七层访问控制
开发语言·windows·python