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

相关推荐
王九思2 分钟前
Java 内存分析工具 MAT
java·开发语言·安全
浅水壁虎13 分钟前
任务调度——XXLJOB2(调度中心)
java·spring boot·spring
青火coding16 分钟前
SOFAServerless架构的意义
java·运维·中间件·架构·serverless
superman超哥20 分钟前
Serde 的零成本抽象设计:深入理解 Rust 序列化框架的哲学
开发语言·rust·开发工具·编程语言·rust序列化
夕除26 分钟前
java--2
java·开发语言
源码获取_wx:Fegn089542 分钟前
计算机毕业设计|基于springboot + vue景区管理系统(源码+数据库+文档)
java·vue.js·spring boot·后端·课程设计
星辰徐哥1 小时前
Rust函数与流程控制——构建逻辑清晰的系统级程序
开发语言·后端·rust
liliangcsdn1 小时前
如何使用lambda对python列表进行排序
开发语言·python
李少兄1 小时前
Git 忽略文件机制:.gitignore 与 .git/info/exclude
java·git·elasticsearch
☀Mark_LY1 小时前
个人数据同步es小工具
java·elasticsearch