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

相关推荐
喵星人工作室5 分钟前
C++传说:神明之剑0.4.5装备机制彻底完成
开发语言·c++·游戏
秦jh_7 分钟前
【Qt】系统相关(下)
开发语言·qt
东木月12 分钟前
使用python获取Windows产品标签
开发语言·windows·python
pumpkin8451413 分钟前
Go 基础语法全景
开发语言·后端·golang
hqwest14 分钟前
码上通QT实战18--监控页面10-获取设备数据
开发语言·qt·湿度·modbus功能码·寄存器地址·从站数据·0103
深海小黄鱼24 分钟前
mysql 导入csv文件太慢, Error Code: 1290.
数据库·mysql
星火开发设计26 分钟前
C++ multiset 全面解析与实战指南
开发语言·数据结构·c++·学习·set·知识
rannn_11134 分钟前
【Java项目】中北大学Java+数据库课设|校园食堂智能推荐与反馈系统
java·数据库·后端·课程设计·中北大学
NE_STOP42 分钟前
SpringBoot-shiro-jwt-dubbo-redis分布式统一权限系统(完结)
java
zfj3211 小时前
java函数式接口 @FunctionalInterface用法
java·函数·function