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

相关推荐
流星白龙9 分钟前
【MySQL】9.MySQL内置函数
android·数据库·mysql
忘忧记11 分钟前
Fixture详解
开发语言·python
赵谨言12 分钟前
地球磁场干扰噪声减弱声波对抗测量系统研究进展:近十年中英文文献综述
大数据·开发语言·经验分享
jyan_敬言13 分钟前
【算法】高精度算法(加减乘除)
c语言·开发语言·c++·笔记·算法
柒.梧.15 分钟前
深入浅出理解原子操作:从单核到多核的实现原理
java
echome88819 分钟前
Python 装饰器实战:用@syntax 优雅地增强函数功能
开发语言·python
Moe48827 分钟前
Redis 缓存三大经典问题:穿透、击穿与雪崩
java·后端·面试
原来是猿28 分钟前
MySQL 在 Centos 7环境安装
数据库·mysql·centos
卷Java39 分钟前
Python字典:键值对、get()方法、defaultdict,附通讯录实战
开发语言·数据库·python
liuyao_xianhui40 分钟前
优选算法_翻转链表_头插法_C++
开发语言·数据结构·c++·算法·leetcode·链表·动态规划