如何从C#中的FTP请求获取IP地址

时间:2011-08-10 20:33:36

标签: c# .net ftp httpwebrequest ip-address

我有一个传入的FTP请求。我想获取传入FTP请求中提到的F​​TP服务器的IP地址。我必须针对列入白名单的FTP服务器进行验证。

任何帮助都将受到赞赏..

我的代码如下:

try
{
    IPHostEntry host;
    string localIP = "?";
    host = Dns.GetHostEntry(uri);
    foreach (IPAddress ip in host.AddressList)
    {
        // we are only interested in IPV4 Addresses
        if (ip.AddressFamily == AddressFamily.InterNetwork) 
        {
            localIP = ip.ToString();
        }
    }

    return localIP;
}
catch (Exception exception)
{
    throw;
}

1 个答案:

答案 0 :(得分:5)

好的,这是我的黑客..

private string GetFTPAddress(string uri)
{
    try
    {
       // IPHostEntry host;
        string localIP = null;
        var entries = uri.Split('/');
        var host = Dns.GetHostAddresses(entries[2]);
        foreach (IPAddress ip in host)
        {
            // we are only interested in IPV4 Addresses
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                localIP = ip.ToString();
            }
        }

        return localIP;
    }
    catch (Exception exception)
    {
        throw;
    }
}
相关问题