publicstaticvoidhttp_post(String url, String port, String path, String content){ /** * @Description: ingest a address and send the content to it. * @Example http://localhost:8428/write + content * @param url url address * @param port remote address port * @param path remote address path * @param content metric data */ String result = ""; try { url = url + ":" + port + "/" + path; System.out.println(url); URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setDoOutput(true); conn.setDoInput(true);
// fill and send content DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.write(content.getBytes()); dos.flush();
// get response (Do not comment this line, or the data insertion will be failed) BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } System.out.println(result);// } catch (Exception e) { System.out.println("Exception," + e.getMessage()); e.printStackTrace(); } }