资源简介
测试是否成功连接到网站,统计成功访问次数和失败访问次数
代码片段和文件信息
package com.util.http;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
public class HttpUtil {
private long total = 0;
private long success = 0;
private long fail = 0;
/**
* logger for this class
*/
private final static Logger logger = Logger.getLogger(HttpUtil.class);
/**
* 获取连接信息
* @param addr
* @return
*/
public String getConnect(String addr){
String result = null;
int status=0;
HttpClient client = new HttpClient();
HttpMethod method=new GetMethod(addr);
try {
status =client.executeMethod(method);
} catch (HttpException e) {
logger.error(e.getMessage()e);
} catch (IOException e) {
logger.error(e.getMessage()e);
}
if(“200“.equals(String.valueOf(status))){
result = “连接成功“;
}else{
result = “连接失败“;
}
return result;
}
/**
* 结果转化
* @param comm
* @return
*/
public String infoFormat(String comm){
String info = null;
Calendar calendar = GregorianCalendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss“);
dateFormat.setTimeZone(java.util.TimeZone.getTim
- 上一篇:anyview 0.2 源代码
- 下一篇:JAVa程序拼图源代码
评论
共有 条评论