简体   繁体   English

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

[英]How to get IP Address from a FTP request in C#

I have an incoming FTP request.我有一个传入的 FTP 请求。 I would like to get the IP Address of the FTP server mentioned in the incoming FTP request.我想获取传入的 FTP 请求中提到的 FTP 服务器的 IP 地址。 I have to validate this against a list of whitelisted FTP servers.我必须根据列入白名单的 FTP 服务器列表来验证这一点。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM