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

发布于 2024-11-29 11:05:27 字数 528 浏览 1 评论 0原文

我有一个传入的 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;
}

I have an incoming FTP request. I would like to get the IP Address of the FTP server mentioned in the incoming FTP request. I have to validate this against a list of whitelisted FTP servers.

Any help will be well appreciated..

My code is as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-12-06 11:05:27

好吧,这是我的黑客..

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

Ok here is my hack..

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文