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

相关推荐
不吃香菜学java5 小时前
Redis的java客户端
java·开发语言·spring boot·redis·缓存
panzer_maus5 小时前
MySQL 索引介绍与索引优化的简单介绍
数据库·mysql
captain3765 小时前
事务___
java·数据库·mysql
北漂Zachary6 小时前
四大编程语言终极对比
android·java·php·laravel
小江的记录本6 小时前
【网络安全】《网络安全常见攻击与防御》(附:《六大攻击核心特性横向对比表》)
java·网络·人工智能·后端·python·安全·web安全
贵沫末6 小时前
python——打包自己的库并安装
开发语言·windows·python
文祐6 小时前
C++类之虚函数表及其内存布局(一个子类继承一个父类)
开发语言·c++
爱学习的小邓同学6 小时前
MySQL --- MySQL数据类型
数据库·mysql
zuowei28896 小时前
华为网络设备配置文件备份与恢复(上传、下载、导出,导入)
开发语言·华为·php
xiaohe077 小时前
超详细 Python 爬虫指南
开发语言·爬虫·python