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

相关推荐
曾凡宇先生18 分钟前
mysql局域网授权
数据库·mysql
江南十四行19 分钟前
Python生成器与协程:从迭代器到异步编程的进阶之路
开发语言·python
代码漫谈32 分钟前
一文学习 SpringBoot 的 application.yml 配置,基于 Spring Boot 3.2.x
java·spring boot·spring·配置文件
SamDeepThinking33 分钟前
程序员如何接受工作内容毫无意义?
java·后端·程序员
测试员周周34 分钟前
【AI测试功能3】AI功能测试的三层架构:单元测试 → 集成测试 → E2E测试——AI系统测试金字塔实战指南
开发语言·人工智能·python·功能测试·架构·单元测试·集成测试
lly20240634 分钟前
AppML 案例原型
开发语言
三翼鸟数字化技术团队1 小时前
基于Redis ZSet实现分布式优先级队列的技术实践
java·redis
jllllyuz1 小时前
MATLAB 回声抵消(AEC)、噪声抑制(NS)、自动增益控制(AGC)完整实现
开发语言·matlab
froginwe111 小时前
Vue.js 计算属性
开发语言
无所事事O_o1 小时前
加密过程及原理浅析
java·加密