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

相关推荐
原来是猿6 分钟前
MySQL【基本查询上 - 表的增删改查】
数据库·mysql
yashuk10 分钟前
【MySQL】表的相关操作
android·mysql·adb
于先生吖14 分钟前
2026 新版 Java 同城上门家政服务系统源码 完整可运营
java·开发语言
MIXLLRED18 分钟前
Python模块详解(一)—— socket 和 threading 模块
开发语言·python·socket·threading
gp32102620 分钟前
什么是Spring Boot 应用开发?
java·spring boot·后端
happymaker062620 分钟前
JDBC(MySQL)——DAY04(调用存储过程,存储函数)
java·数据库·mysql
csbysj202022 分钟前
桥接模式(Bridge Pattern)
开发语言
清空mega23 分钟前
第7章:JavaBean、Servlet 与 MVC——从 JSP 页面开发走向规范项目
java·servlet·mvc
Yupureki34 分钟前
《C++实战项目-高并发内存池》4.CentralCache构造
c语言·开发语言·c++·单例模式·github
2401_8980751237 分钟前
分布式系统监控工具
开发语言·c++·算法