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

相关推荐
CoderCodingNo33 分钟前
【GESP】C++五级考试大纲知识点梳理, (5) 算法复杂度估算(多项式、对数)
开发语言·c++·算法
倚栏听风雨34 分钟前
java.lang.SecurityException异常
java
星河队长39 分钟前
VS创建C++动态库和C#访问过程
java·c++·c#
三三木木七1 小时前
mysql拒绝连接
数据库·mysql
唐古乌梁海1 小时前
【mysql】InnoDB的聚簇索引和非聚簇索引工作原理
数据库·mysql
꒰ঌ 安卓开发໒꒱1 小时前
Mysql 坏表修复
android·mysql·adb
鼠鼠我捏,要死了捏1 小时前
Java虚拟线程原理与性能优化实战
java·performance-optimization·virtual-thread
ftpeak1 小时前
JavaScript性能优化实战
开发语言·javascript·性能优化
艾菜籽1 小时前
Spring MVC练习:留言板
java·spring·mvc
一个很帅的帅哥2 小时前
JavaScript事件循环
开发语言·前端·javascript