如题 怎么用来测试一个网站的延时 类似于ping的功能 来检测网站的响应时间jdk里自带有函数吗? 

解决方案 »

  1.   


    exec 里的参数是网址? 
      

  2.   

    package org.csdn;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;public class TimerExecPing
    {
    public static void main(String args[]) throws IOException
    { TimerTask timerTask = new TimerTask()
    {
    public void run()
    {
    Process p;
    try
    {
    p = Runtime.getRuntime().exec("ping www.sina.com.cn");
    String result = "";
    BufferedReader input = new BufferedReader(
    new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = input.readLine()) != null)
    {
    result += line;
    }
    input.close();
    System.out.println(result);
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }; Timer timer = new Timer();
    timer.schedule(timerTask, new Date());
    }
    }
      

  3.   


    这个方法只是测试用,时间上的设置改写schedule方法可以。另外不用的时候调用p.destroy()。
      

  4.   

    呵呵 谢谢了 不过你的程序有些小问题 
    中文字符是乱码   还有回显的信息阅读不方便 嘿嘿 这是我的根据你的修改的     public static void main(String args[]) throws IOException
        { TimerTask timerTask = new TimerTask()
    {
        public void run()
        {
    Process p;
    try
    {
        p = Runtime.getRuntime().exec("ping www.taobao.com.cn  -t");
        String result = "";
        BufferedReader input = new BufferedReader( new InputStreamReader(p.getInputStream(),"gbk"));
        String line;
        while ((line = input.readLine()) != null)
        {
    //result += line;
    System.out.println(line);
        }
        input.close();
        //System.out.println(result);
    } catch (IOException e)
    {
        e.printStackTrace();
    }
        }
    }; Timer timer = new Timer();
    timer.schedule(timerTask, new Date());
        }