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

相关推荐
jzlhll123几秒前
kotlin Flow first() last()总结
开发语言·前端·kotlin
小涛不学习几秒前
Spring Boot 详解(从入门到原理)
java·spring boot·后端
W.D.小糊涂1 分钟前
gpu服务器安装windows+ubuntu24.04双系统
c语言·开发语言·数据库
用头发抵命22 分钟前
Vue 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
似水明俊德36 分钟前
02-C#.Net-反射-学习笔记
开发语言·笔记·学习·c#·.net
顶点多余41 分钟前
使用C/C++语言链接Mysql详解
数据库·c++·mysql
于先生吖1 小时前
Java框架开发短剧漫剧系统:后台管理与接口开发
java·开发语言
khddvbe1 小时前
C++并发编程中的死锁避免
开发语言·c++·算法
daidaidaiyu2 小时前
Spring IOC 源码学习 声明式事务的入口点
java·spring
myloveasuka2 小时前
[Java]查找算法&排序算法
java·算法·排序算法