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

相关推荐
Hello.Reader4 分钟前
PySpark 依赖管理集群环境下如何分发 Python 包
开发语言·python
七七powerful6 分钟前
MySQL 8.0 性能优化利器:Percona Toolkit 实战指南
数据库·mysql·性能优化
南境十里·墨染春水8 小时前
C++传记(面向对象)虚析构函数 纯虚函数 抽象类 final、override关键字
开发语言·c++·笔记·算法
无巧不成书02188 小时前
30分钟入门Java:从历史到Hello World的小白指南
java·开发语言
2301_797172758 小时前
基于C++的游戏引擎开发
开发语言·c++·算法
成为大佬先秃头9 小时前
数据库连接池:Druid
数据库·mysql·druid
比昨天多敲两行9 小时前
C++ 二叉搜索树
开发语言·c++·算法
zs宝来了10 小时前
Playwright 自动发布 CSDN 的完整实践
java
Birdy_x10 小时前
接口自动化项目实战(1):requests请求封装
开发语言·前端·python
海海不瞌睡(捏捏王子)10 小时前
C++ 知识点概要
开发语言·c++