import java.io.*;
import java.text.SimpleDateFormat;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javafx.scene.chart.PieChart;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
public class httptest {
private static final Logger log = Logger.getLogger(httptest.class);
private static SimpleDateFormat df;
public static String sendPost(String urlParam) throws HttpException, IOException {
// 创建httpClient实例对象
HttpClient httpClient = new HttpClient();
// 设置httpClient连接主机服务器超时时间:15000毫秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
// 创建post请求方法实例对象
PostMethod postMethod = new PostMethod(urlParam);
// 设置post请求超时时间
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
postMethod.addRequestHeader("Content-Type", "application/json");
httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
return result;
}
public static String sendGet(String urlParam) throws HttpException, IOException {
// 创建httpClient实例对象
HttpClient httpClient = new HttpClient();
// 设置httpClient连接主机服务器超时时间:15000毫秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
// 创建GET请求方法实例对象
GetMethod getMethod = new GetMethod(urlParam);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 设置post请求超时时间
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
getMethod.addRequestHeader("Content-Type", "application/json");
httpClient.executeMethod(getMethod);
String result = getMethod.getResponseBodyAsString();
getMethod.releaseConnection();
return result;
}
private static void montiot(String JsonText, PrintWriter pw) {
JSONObject object = JSONObject.parseObject(JsonText);
//获取网页Json
JSONArray arr = JSONObject.parseArray(object.getString("beans"));
//获取json
JSONObject object1 = JSONObject.parseObject(arr.getJSONObject(32).getString("LiveNodes"));
//获取json数组
String[] str={};
//获取json数组的key数组
for(String st:str){
//循环遍历
JSONObject object2 = JSONObject.parseObject(object1.getString(st));
String infoAddr=object2.getString("infoAddr").substring(0,object2.getString("infoAddr").length()-6);
String lastContact=object2.getString("lastContact");
if(Integer.parseInt(lastContact)>=10){
//判断超时条件
String str1 = df.format(System.currentTimeMillis())+","+st+","+infoAddr+","+lastContact;
pw.println(str1);
System.out.println(str1);
}
}
}
public static void main(String[] args) throws HttpException, IOException, InterruptedException {
PrintWriter pw =new PrintWriter(
new OutputStreamWriter(new FileOutputStream("C:\\Ms\\doumi\\log\\montiot.txt",true),"utf-8"),true);
//输出向日志文件的流
String url ="";
//url
while(true) {
//无限循环
montiot(sendGet(url),pw);
Thread.sleep(10000);
}
}
}