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

相关推荐
咸鱼2.08 分钟前
【java入门到放弃】需要背诵
java·开发语言
ZK_H9 分钟前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐14 分钟前
【C++第二十九章】IO流
开发语言·c++
椰猫子19 分钟前
Java:异常(exception)
java·开发语言
lifewange22 分钟前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
cmpxr_1 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
2401_827499991 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉1 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
win x1 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
星晨雪海1 小时前
基于 @Resource 的支付 Service 多实现类完整示例
java·开发语言