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

相关推荐
钱多多_qdd5 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
XiaoH2335 分钟前
培训机构Day15
sql·mysql
waicsdn_haha7 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
_WndProc9 分钟前
C++ 日志输出
开发语言·c++·算法
Q_192849990617 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
qq_4335545418 分钟前
C++ 面向对象编程:+号运算符重载,左移运算符重载
开发语言·c++
Code_流苏20 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
ThisIsClark26 分钟前
【后端面试总结】MySQL主从复制逻辑的技术介绍
mysql·面试·职场和发展
数据小爬虫@37 分钟前
如何高效利用Python爬虫按关键字搜索苏宁商品
开发语言·爬虫·python
ZJ_.39 分钟前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps