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资料站22 分钟前
Docker 快速部署 MySQL 主从复制(一主一从)
mysql·adb·docker
Dxy12393102164 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
我学上瘾了4 小时前
Spring Cloud的前世今生
后端·spring·spring cloud
故事和你915 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
techdashen6 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
一 乐6 小时前
电影院|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·电影院购票管理管理系统
星空椰6 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
恼书:-(空寄6 小时前
JVM GC 日志分析 + 常见 GC 场景 + 实战参数调优
java·jvm
消失的旧时光-19436 小时前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
忒可君6 小时前
C# winform 自制分页功能
android·开发语言·c#