java 报文请求_Java 用HTTP的方式发送JSON报文请求

前言:

项目调用第三方接口时,通常是用socket或者http的通讯方式发送请求:http 为短连接,客户端发送请求都需要服务器端回送响应,请求结束后,主动释放链接。Socket为长连接:通常情况下Socket 连接就是 TCP 连接,因此 Socket 连接一旦建立,通讯双方开始互发数据内容,直到双方断开连接。下面介绍HTTP的方式发送和接收JSON报文。

需求:

用HTTP的方式,向URL为10.10.10.110:8888地址发送json报文,返回的结果也是json报文。

a70800d14bc0b85a82877eb5285de508.png

主要代码如下:

48116908e982fd952a1c235d7667e70e.gif

1 String resp= null;

2 JSONObject obj = new JSONObject();

3 obj.put("name", "张三");

4 obj.put("age", "18");

5 String query = obj.toString();

6 log.info("发送到URL的报文为:");

7 log.info(query);

8 try {

9 URL url = new URL("http://10.10.10.110:8888"); //url地址

10

11 HttpURLConnection connection = (HttpURLConnection) url.openConnection();

12 connection.setDoInput(true);

13 connection.setDoOutput(true);

14 connection.setRequestMethod("POST");

15 connection.setUseCaches(false);

16 connection.setInstanceFollowRedirects(true);

17 connection.setRequestProperty("Content-Type","application/json");

18 connection.connect();

19

20 try (OutputStream os = connection.getOutputStream()) {

21 os.write(query.getBytes("UTF-8"));

22 }

23

24 try (BufferedReader reader = new BufferedReader(

25 new InputStreamReader(connection.getInputStream()))) {

26 String lines;

27 StringBuffer sbf = new StringBuffer();

28 while ((lines = reader.readLine()) != null) {

29 lines = new String(lines.getBytes(), "utf-8");

30 sbf.append(lines);

31 }

32 log.info("返回来的报文:"+sbf.toString());

33 resp = sbf.toString();

34

35 }

36 connection.disconnect();

37

38 } catch (Exception e) {

39 e.printStackTrace();

40 }finally{

41 JSONObject json = (JSONObject)JSON.parse(resp);

42 }

062e8e7a1d629d6fe2a56c0175f2a681.gif

网上还有一种拼json发送报文的方式,也把代码分享出来:

46fcfd050f10feeeb58c42ee079ba6fc.gif

1 String resp = null;

2 String name = request.getParameter("userName");

3 String age = request.getParameter("userAge");

4 String query = "{\"name\":\""+name+"\",\"age\":\""+age+"\"}";

5

6 try {

7 URL url = new URL("http://10.10.10.110:8888"); //url地址

8

9 HttpURLConnection connection = (HttpURLConnection) url.openConnection();

10 connection.setDoInput(true);

11 connection.setDoOutput(true);

12 connection.setRequestMethod("POST");

13 connection.setUseCaches(false);

14 connection.setInstanceFollowRedirects(true);

15 connection.setRequestProperty("Content-Type","application/json");

16 connection.connect();

17

18 try (OutputStream os = connection.getOutputStream()) {

19 os.write(query.getBytes("UTF-8"));

20 }

21

22 try (BufferedReader reader = new BufferedReader(

23 new InputStreamReader(connection.getInputStream()))) {

24 String lines;

25 StringBuffer sbf = new StringBuffer();

26 while ((lines = reader.readLine()) != null) {

27 lines = new String(lines.getBytes(), "utf-8");

28 sbf.append(lines);

29 }

30 log.info("返回来的报文:"+sbf.toString());

31 resp = sbf.toString();

32

33 }

34 connection.disconnect();

35

36 } catch (Exception e) {

37 e.printStackTrace();

38 }finally{

39 JSONObject json = (JSONObject)JSON.parse(resp);

40 }

857674ce79c700efedd755acd41f9c3e.gif

两种方式其实都是一样的。写得不对的地方,往各位撸过的大拿指正~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值