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

标签 c# .net ftp httpwebrequest ip-address

我有一个传入的 FTP 请求。我想获取传入 FTP 请求中提到的 FTP 服务器的 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;
}

最佳答案

好的,这是我的技巧..

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;
    }
}

关于c# - 如何从 C# 中的 FTP 请求获取 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7017418/

相关文章:

c# - 在不使用私有(private)成员的情况下保持 System.Threading.Timer 处于事件状态

c# - WPF MVVM切换焦点在2个文本框之间

c# - C++ 中的 XAML 桌面应用程序(Visual Studio Ultimate 2012 RC)

c# - C# 中的正则表达式电子邮件验证器中的问题

c# - 如何让 RabbitMQ 从队列中一个一个读取?

c++ - 好的免费 FTP 客户端库(用于 Windows C++ 商业应用程序)?

c# - 如何在 Entity Framework 中返回嵌套列表

.net - F# 处理从数据库返回的空记录

php - 如何通过 FTP 上传核心 PHP 文件而不打断网站访问者

php ftp_get 在失败时删除本地文件