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

相关推荐
2401_832131952 分钟前
模板编译期机器学习
开发语言·c++·算法
嵌入小生0072 分钟前
Data Structure Learning: Starting with C Language Singly Linked List
c语言·开发语言·数据结构·算法·嵌入式软件
云和数据.ChenGuang2 分钟前
python对接mysql和模型类的故障
数据库·python·mysql·oracle·conda·virtualenv
独自破碎E7 分钟前
LCR005-最大单词长度乘积
java·开发语言
2401_838472518 分钟前
单元测试在C++项目中的实践
开发语言·c++·算法
码农阿豪9 分钟前
影刀RPA与Java融合实践:打造高效智能的高铁票务解决方案
java·python·rpa
_F_y11 分钟前
MySQL用户管理
android·mysql·adb
naruto_lnq14 分钟前
移动语义与完美转发详解
开发语言·c++·算法
龚礼鹏16 分钟前
图像显示框架十一——BufferQueue的工作流程(基于Android 15源码分析)
java·网络·数据库
爬山算法17 分钟前
Hibernate(72)如何在NoSQL数据库中使用Hibernate?
java·nosql·hibernate