Ping域C#

时间:2012-01-15 21:56:38

标签: c# web ping

尝试ping一个域,看看我是否收到响应代码,以获取其注册的指示。从以下代码获得持续的积极结果 - 任何想法?

public static string Check(string keyword)
    {
        Ping pingSender = new Ping();
        PingOptions options = new PingOptions();

        // Use the default Ttl value which is 128,
        // but change the fragmentation behavior.
        options.DontFragment = true;

        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        PingReply reply = pingSender.Send(keyword, timeout, buffer, options);
        if (reply.Status == IPStatus.Success)
        {

            return "found";
        }
        else
        {
            return "not found";
        }
    }


    private void hunt_Click(object sender, EventArgs e)
    {
        string keyword = txtKeyword.Text;
        txtOutput.Text = Check(keyword);
    }

感谢任何帮助: - )

1 个答案:

答案 0 :(得分:0)

嘿,我运行此代码并且它可以工作,(输入worng IP或DNS时抛出异常)为什么不使用这个重载?

       public static string Check(string keyword)
         {
        Ping pingSender = new Ping();
        //PingOptions options = new PingOptions();

        // Use the default Ttl value which is 128, 
        // but change the fragmentation behavior. 
       // options.DontFragment = true;

        // Create a buffer of 32 bytes of data to be transmitted. 
        //string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        // byte[] buffer = Encoding.ASCII.GetBytes(data);
        // int timeout = 120;
        try
        {
            PingReply reply = pingSender.Send(keyword);
            return "found";
        }
        catch
        {
            return "not found";
        }


    }