Java测试域名稳定性

使用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();
		}
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牟云飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值