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

相关推荐
时艰.2 分钟前
JVM 垃圾收集器ParNew&CMS与三色标记算法
java·jvm·算法
百***07454 分钟前
小米MiMo-V2-Flash深度解析:国产开源大模型标杆+一步API接入全指南
java·大数据·开源·php
信也科技布道师4 分钟前
基石Redis实例自动化调度之路
java·开发语言·redis·自动化
PD我是你的真爱粉10 分钟前
MySQL基础-DDL语句
数据库·mysql·oracle
666HZ66619 分钟前
程序设计竞赛java
java·开发语言
三不原则19 分钟前
AIOps 技术架构全景:数据采集→分析→自动化执行全流程
java·架构·自动化
开发者小天20 分钟前
python查询天气小示例
开发语言·python
知行合一。。。21 分钟前
Python--04--数据容器(元组)
开发语言·python