使用httpClient建立链接,每次链接建立间隔时间为100ms,如果状态200表示成功,所有访问结果记录到H:\net.txt中
package com.muyunfei;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 测试域名稳定性
* @author 牟云飞
* @QQ 1147417467
*
*/
public class TestMain {
public static void main(String[] args) {
String noteUrl="这里输入域名地址";//------------------------------------
//调用结果
while(1==1) {
//循环调用
try {
//调用间隔毫秒数
Thread.sleep(100);
//发起链接调用
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost= new HttpPost(noteUrl);
// Create a custom response handler
ResponseHandler<JSONObject> responseHandler = new ResponseHandler<JSONObject>() {
//对访问结果进行处理
public JSONObject handleResponse(
final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
SimpleDateFormat sd = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
System.out.println(sd.format( new Date())+":SUCCESS "+status);
TestMain.writeLog(sd.format( new Date())+":SUCCESS "+status);
if(null!=entity){
String result= EntityUtils.toString(entity);
//根据字符串生成JSON对象
JSONObject resultObj = JSONObject.fromObject(result);
return resultObj;
}else{
return null;
}
} else {
System.out.println(sd.format(new Date())+":Unexpected response status: " + status);
TestMain.writeLog(sd.format(new Date())+":Unexpected response status: " + status);
throw new ClientProtocolException(sd.format(new Date())+":Unexpected response status: " + status);
}
}
};
//返回的json对象
httpclient.execute(httpPost, responseHandler);
httpclient.close();
} catch (Exception e) {
e.printStackTrace();
SimpleDateFormat sd = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
System.out.println(sd.format(new Date())+":"+e.getMessage());
TestMain.writeLog(sd.format(new Date())+":"+e.getMessage());
}
}
}
public static FileWriter output = null;
public static BufferedWriter bf = null;
/**
* 记录日志到H://net.txt目录下
* @param log
*/
public static void writeLog(String log){
try {
if(null==output)
output= new FileWriter("H://net.txt");
if(null==bf)
bf= new BufferedWriter(output);
bf= new BufferedWriter(output);
bf.write(log + "\r\n");
bf.flush();// 清空通道
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}