一、发送json
public void test() throws IOException { //参数封装------------------------------------------------------ Map jsonMap = new HashMap(); Map params1 = new HashMap(); List params2=new ArrayList<>(); //封装params1 //封装params2 //封装jsonMap jsonMap.put("sendBasic", params1); jsonMap.put("toUsers", params2); String json = JSON.toJSONString(jsonMap); //---建立连接--------------------------------------------------------------- URL url = new URL("http://");//请求的路径 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); // 设置可输入 connection.setDoOutput(true); // 设置该连接是可以输出的 connection.setRequestMethod("POST");// 设置请求方式 connection.setRequestProperty("Charset", "UTF-8"); connection.setRequestProperty("Accept-Charset", "utf-8"); connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); //发送参数--------------------------------------------------------------------------- BufferedOutputStream pw = new BufferedOutputStream(connection.getOutputStream()); //发送内容转码utf-8 **必写** pw.write(json.getBytes("UTF-8")); pw.flush(); pw.close(); //读取响应数据--------------------------------------------------------------------------- //输入流转码utf-8 **必写** BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line = null; StringBuilder result = new StringBuilder(); while ((line = br.readLine()) != null) { // 读取数据 result.append(line + "\n"); } }