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

相关推荐
cch89187 小时前
易语言与C++:编程语言终极对决
开发语言·c++
两点王爷8 小时前
docker 创建和使用存储卷相关内容
java·docker·容器
boonya8 小时前
Embedding模型与向量维度动态切换完整方案
java·数据库·embedding·动态切换大模型
shark22222228 小时前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
宁波阿成8 小时前
族谱管理系统架构分析与亮点总结
java·系统架构·vue·ruoyi-vue·族谱
姬成韶8 小时前
BUUCTF--[RoarCTF 2019]Easy Java
java·网络安全
组合缺一8 小时前
Solon AI Harness 首次发版
java·人工智能·ai·llm·agent·solon
551只玄猫8 小时前
【数学建模 matlab 实验报告6】行遍性问题
开发语言·数学建模·matlab
回到原点的码农8 小时前
Linux(CentOS)安装 MySQL
linux·mysql·centos
m0_491267008 小时前
Docker部署MySQL 多库自动备份(结构+数据完整,适配CentOS)
mysql·安全