zoukankan      html  css  js  c++  java
  • Java 用HTTP的方式发送JSON报文请求

    前言: 

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

    需求:

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

    主要代码如下:

    String resp= null;
                JSONObject obj = new JSONObject();
                obj.put("name", "张三");   
                obj.put("age", "18");   
                String query = obj.toString();
                log.info("发送到URL的报文为:");
                log.info(query);
                try {
                    URL url = new URL("http://127.0.0.1:8888"); //url地址
    
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setRequestMethod("POST");
                    connection.setUseCaches(false);
                    connection.setInstanceFollowRedirects(true);
                    connection.setRequestProperty("Content-Type","application/json");
                    connection.connect();
    
                    try (OutputStream os = connection.getOutputStream()) {
                        os.write(query.getBytes("UTF-8"));
                    }
    
                    try (BufferedReader reader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()))) {
                        String lines;
                        StringBuffer sbf = new StringBuffer();
                        while ((lines = reader.readLine()) != null) {
                            lines = new String(lines.getBytes(), "utf-8");
                            sbf.append(lines);
                        }
                        log.info("返回来的报文:"+sbf.toString());
                        resp = sbf.toString();    
                       
                    }
                    connection.disconnect();
    
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    JSONObject json = (JSONObject)JSON.parse(resp);
                }

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

    String resp = null;
    String name = request.getParameter("userName");
    String age = request.getParameter("userAge");
    String query = "{"name":""+name+"","age":""+age+""}";
    
    try {
                    URL url = new URL("http://10.10.10.110:8888"); //url地址
    
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setRequestMethod("POST");
                    connection.setUseCaches(false);
                    connection.setInstanceFollowRedirects(true);
                    connection.setRequestProperty("Content-Type","application/json");
                    connection.connect();
    
                    try (OutputStream os = connection.getOutputStream()) {
                        os.write(query.getBytes("UTF-8"));
                    }
    
                    try (BufferedReader reader = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()))) {
                        String lines;
                        StringBuffer sbf = new StringBuffer();
                        while ((lines = reader.readLine()) != null) {
                            lines = new String(lines.getBytes(), "utf-8");
                            sbf.append(lines);
                        }
                        log.info("返回来的报文:"+sbf.toString());
                        resp = sbf.toString();    
                       
                    }
                    connection.disconnect();
    
                } catch (Exception e) {
                    e.printStackTrace();
                }finally{
                    JSONObject json = (JSONObject)JSON.parse(resp);
                }

    两种方式其实都是一样的。

  • 相关阅读:
    腾讯QQ强制下线,每天可能强抢会员300万元
    visual studio 2010 winform程序不能添加对system.web的引用
    一个用来将Excel中同一个人的多行记录中的特定列合并到第一行的宏
    很希望看到微软来一次“因为QQ未经用户允许扫描硬盘,在所有window平台拒绝运行QQ”
    windows2003的数据执行保护会造成无法正常安装SQL SERVER
    改变jboss部署目录(虚拟目录)
    SecureCRT6.5.0英文版,提示数据库里没有找到防火墙"无"
    spring注解事务
    windows下查看端口被哪个程序占用
    union和union all区别
  • 原文地址:https://www.cnblogs.com/wqsbk/p/6771045.html
Copyright © 2011-2022 走看看