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

相关推荐
%Leo几秒前
macos idea 插件搜索不到
java·intellij-idea
苏渡苇几秒前
枚举的高级用法——用枚举实现策略模式和状态机
java·单例模式·策略模式·枚举·状态机·enum
鱼鳞_1 分钟前
Java学习笔记_Day19
java·笔记·学习
画堂秋3 分钟前
云原生-Mysql
运维·mysql·云原生
怪我冷i4 分钟前
在win11进行Rust Web 开发,采用Salvo框架
开发语言·前端·rust
吴声子夜歌6 分钟前
Node.js——os操作系统模块
开发语言·node.js·php
曹牧6 分钟前
Java:驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立连接
java·开发语言·ssl
cch89187 分钟前
PHP vs C++:10倍性能差距的编程语言对决
android·java·开发语言
司马万7 分钟前
RUST基础1----数据类型
开发语言·算法·rust
程序员buddha7 分钟前
Spring集合注入功能
windows·python·spring