public static void sendPost_body2(String urlparam, JSONArray userArray) { try { URL url = new URL(urlparam); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); connection.connect(); //POST请求 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); System.out.println(userArray.toString()); out.write(userArray.toString().getBytes("UTF-8")); out.flush(); out.close(); //读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String lines; StringBuffer sb = new StringBuffer(""); while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); sb.append(lines); } System.out.println(sb); reader.close(); // 断开连接 connection.disconnect(); } catch ( MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch ( UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch ( IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
java httpClient 传参JSONArray
最新推荐文章于 2024-07-15 03:14:41 发布