Java 用HTTP的方式发送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://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);
            }

网上还有一种拼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);
            }

猜你喜欢

转载自blog.csdn.net/atongmu2017/article/details/82180668
今日推荐