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

相关推荐

  1. http发送post请求发送参数实体

    2024-03-18 15:42:03       46 阅读
  2. electron发送post请求

    2024-03-18 15:42:03       43 阅读
  3. 深入理解通用的HTTP POST请求发送方法

    2024-03-18 15:42:03       57 阅读
  4. Python requests get和post方法发送HTTP请求

    2024-03-18 15:42:03       65 阅读
  5. RestTemplate发送https请求

    2024-03-18 15:42:03       54 阅读
  6. Linux发送HTTP请求

    2024-03-18 15:42:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 15:42:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 15:42:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 15:42:03       82 阅读
  4. Python语言-面向对象

    2024-03-18 15:42:03       91 阅读

热门阅读

  1. 2022蓝桥杯/修剪灌木/c\c++

    2024-03-18 15:42:03       42 阅读
  2. linux: network: sysctl: netdev_budget_usecs的出现

    2024-03-18 15:42:03       36 阅读
  3. hive库表占用空间大小的命令

    2024-03-18 15:42:03       45 阅读
  4. 数据库的约束

    2024-03-18 15:42:03       43 阅读
  5. 2024-Centos7安装PostgreSql14

    2024-03-18 15:42:03       36 阅读
  6. C++ primer 第十四章

    2024-03-18 15:42:03       38 阅读
  7. CSS浮动

    CSS浮动

    2024-03-18 15:42:03      37 阅读
  8. 免费SSL证书Let‘sEncrypt安装

    2024-03-18 15:42:03       42 阅读
  9. C#基础之异常处理

    2024-03-18 15:42:03       35 阅读
  10. Python中的with语句以及它的用途。

    2024-03-18 15:42:03       40 阅读
  11. Kafka 实现之分配(Distribution)

    2024-03-18 15:42:03       33 阅读