java http 发送json数据_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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值