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

相关推荐
我居然是兔子17 小时前
异常练习:在试错中吃透Java异常处理的底层逻辑
java·开发语言
养一回月亮!17 小时前
使用Qt实现简单绘图板:鼠标绘制与擦除功能详解
开发语言·qt
BanyeBirth17 小时前
C++差分数组(二维)
开发语言·c++·算法
Tony Bai18 小时前
Go 的 AI 时代宣言:我们如何用“老”原则,解决“新”问题?
开发语言·人工智能·后端·golang
Fcy64818 小时前
C++ map和multimap的使用
开发语言·c++·stl
CC.GG18 小时前
【C++】STL容器----unordered_map和unordered_set的使用
java·数据库·c++
L Jiawen18 小时前
【Golang基础】基础知识(下)
服务器·开发语言·golang
Overt0p19 小时前
抽奖系统(4)
java·spring boot·tomcat
编程小Y19 小时前
如何优化MySQL的查询性能?
数据库·mysql
云栖梦泽19 小时前
鸿蒙应用AI赋能与国际化落地实战:让待办应用跨越语言与智能边界
开发语言·鸿蒙系统