Pinging Domain C#

本文关键字:Domain Pinging | 更新日期: 2023-09-27 18:20:26

尝试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);
    }

如有任何帮助,我们将不胜感激:-)

Pinging Domain C#

嘿,我运行了这个代码,它能工作,(当输入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";
        }

    }