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

相关推荐
焦糖玛奇朵婷16 小时前
解锁扭蛋机小程序的五大优势
java·大数据·服务器·前端·小程序
山栀shanzhi16 小时前
C/C++之:构造函数为什么不能设置为虚函数?
开发语言·c++·面试
lsx20240616 小时前
.toggleClass() 方法详解
开发语言
yuan1999716 小时前
C&CG(列与约束生成)算法,来解决“风光随机性”下的微网鲁棒配置问题
c语言·开发语言·算法
SamDeepThinking17 小时前
别让一个超时的第三方http接口拖垮所有接口
java·后端·架构
李白的天不白17 小时前
读到数据为undefind是的几种情况
开发语言·javascript·ecmascript
YaBingSec17 小时前
玄机靶场:供应链安全-供应链应急-Part2 通关笔记
java·笔记·安全
Gerardisite17 小时前
企微机器人开发指南
java·python·机器人·自动化·企业微信
OtIo TALL17 小时前
Java进阶(ElasticSearch的安装与使用)
java·elasticsearch·jenkins
LeocenaY17 小时前
C语言面试题总结
c语言·开发语言·数据结构