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 小时前
量子计算在网络安全中的应用
开发语言·javascript·爬虫·flutter
superman超哥5 小时前
Rust 线程安全性保证(Send 与 Sync):编译期并发安全的类型系统
开发语言·后端·rust·编程语言·并发安全·send与sync·rust线程
倔强的小石头_5 小时前
Python 从入门到实战(十八):学生成绩系统高级功能实战(实时通知与数据看板)
开发语言·python
indexsunny5 小时前
互联网大厂Java求职面试实战:Spring Boot与微服务在电商场景中的应用解析
java·数据库·spring boot·微服务·面试·消息队列·电商
亮子AI5 小时前
【JavaScript】forEach 是按数组顺序执行吗?
开发语言·javascript·ecmascript
菩提祖师_5 小时前
基于Docker的微服务自动化部署系统
开发语言·javascript·flutter·docker
廋到被风吹走5 小时前
【Java】【JVM】内存模型
java·开发语言·jvm
talenteddriver5 小时前
java: JAVA静态方法细节
java·前端·apache
小杰s5 小时前
linux下mysql忘记密码
linux·mysql·adb
indexsunny5 小时前
互联网大厂Java面试实录:从Spring Boot到微服务实战解析
java·spring boot·spring cloud·kafka·microservices·java interview·software development