public String clusterJson() {
String result = null;
try {
String str = Jsoup.connect("http://192.168.3.186/api/monitoring/v1/clusters")
.userAgent("Mozilla/5.0").timeout(10 * 1000).method(Method.POST)
.header("Content-Type", "application/json")
.header("kbn-name", "kibana") //Kinbana有header检测,需要包含此项
.header("kbn-version", "5.6.3")
.data("a", "b") // 如果不加 data,requestBody 不会当json格式处理 而是默认格式
.requestBody("{\"timeRange\":{\"min\":\"2018-01-15T07:05:37.504Z\",\"max\":\"2018-01-15T08:05:37.505Z\"}}").followRedirects(true).ignoreContentType(true)
.execute().body();
result = str.substring(1, str.length() - 1); //去除首尾的[]
} catch (IOException ioe) {
logger.error("MonitorElasticSearch clusterJson() post Es 获取clusters json 失败", ioe);
}
return result;
}
示例为发送post请求 从Kinbana获取clusters json格式信息
注意jsoup版本至少1.9.1 否则不包含requestBody方法