java使用httpcomponents post发送json数据

一、适用场景

  当我们向第三方系统提交数据的时候,需要调用第三方系统提供的接口。不同的系统提供的接口也不一样,有的是SOAP Webservice、RESTful Webservice 或其他的。当使用的是RESTful webservice的时候,就可以使用httpcomponents组件来完成调用。

  如我们需要发起post请求,并将数据转成json格式,设置到post请求中并提交。

  url:"http://www.xxxxx.com/message"

  json数据格式 {"name":"zhangsan", "age":20, "gender": "mail"}   // 一个用户的基本信息

二、实例代码

 1 package com.demo.test;
 2 
 3 import java.io.IOException;
 4 
 5 import org.apache.http.HttpEntity;
 6 import org.apache.http.client.ClientProtocolException;
 7 import org.apache.http.client.methods.CloseableHttpResponse;
 8 import org.apache.http.client.methods.HttpPost;
 9 import org.apache.http.entity.ContentType;
10 import org.apache.http.entity.StringEntity;
11 import org.apache.http.impl.client.CloseableHttpClient;
12 import org.apache.http.impl.client.HttpClients;
13 import org.apache.http.util.EntityUtils;
14 
15 public class Test {
16 
17     public static String sendInfo(String sendurl, String data) {
18         CloseableHttpClient client = HttpClients.createDefault();
19         HttpPost post = new HttpPost(sendurl);
20         StringEntity myEntity = new StringEntity(data,
21                 ContentType.APPLICATION_JSON);// 构造请求数据
22         post.setEntity(myEntity);// 设置请求体
23         String responseContent = null; // 响应内容
24         CloseableHttpResponse response = null;
25         try {
26             response = client.execute(post);
27             if (response.getStatusLine().getStatusCode() == 200) {
28                 HttpEntity entity = response.getEntity();
29                 responseContent = EntityUtils.toString(entity, "UTF-8");
30             }
31         } catch (ClientProtocolException e) {
32             e.printStackTrace();
33         } catch (IOException e) {
34             e.printStackTrace();
35         } finally {
36             try {
37                 if (response != null)
38                     response.close();
39 
40             } catch (IOException e) {
41                 e.printStackTrace();
42             } finally {
43                 try {
44                     if (client != null)
45                         client.close();
46                 } catch (IOException e) {
47                     e.printStackTrace();
48                 }
49             }
50         }
51         return responseContent;
52     }
53 
54     public static void main(String[] args) {
55         String json = "{"name":"zhangsan", "age":20, "gender": "mail"} ";
56         String result = sendInfo("http://www.xxxxx.com/message", json);
57         System.out.println(result);
58     }
59 }

  发送请求之后,后台会打印返回的信息。

原文地址:https://www.cnblogs.com/always-online/p/3899421.html