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

相关推荐
zx_741484811 小时前
【Python 入门】面向对象基础:类、对象、成员变量与构造方法
开发语言·python
月华路2 小时前
G1 GC 对数组与大对象(Humongous)的处理
java·jvm·算法
好好学习的星熊2 小时前
SQL 面试题(数据分析师)
sql·mysql
liangshanbo12152 小时前
虚拟列表深度面试题整理
java·开发语言·前端
ZISHU_9872 小时前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉
破土士V2 小时前
MySQL基础知识集合
数据库·mysql
略略略咯咯3 小时前
C#Unity
开发语言·unity·c#
gugucoding3 小时前
59. 【Java】Spring Boot 入门:第一个 Web 应用
java·开发语言·spring boot
lilian2333 小时前
Harmony os 技术实战|拼豆制图43:压缩字符矩阵上线前如何拦住错位图纸
开发语言·前端·华为·矩阵·harmonyos
进击ing小白3 小时前
Qt 无边框窗口进阶指南
开发语言·qt