【问题标题】:How to get IP Address from a FTP request in C#如何从 C# 中的 FTP 请求中获取 IP 地址
【发布时间】:2011-10-24 10:47:20
【问题描述】:

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

【问题讨论】:

  • 好吧,我使用的大部分代码都来自stackoverflow。这些是我关注的链接stackoverflow.com/questions/1069103/…blogs.x2line.com/al/archive/2008/08/29/3544.aspx
  • 请您澄清/编辑,以便我确切知道您需要什么?
  • 您是在为 FTP 请求提供服务还是在发出请求?您发布的代码完全不相关。
  • 我正在发出 FTP 请求。我拥有的代码是我如何尝试获取给定 FTP URL 的 IP 地址。 FtpWebRequest ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath));
  • 那么这段代码应该可以工作 - 您正在做的是将地址过滤为可能正在过滤您的实际服务器的 IPv4 地址。

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


【解决方案1】:

好的,这是我的 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;
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2012-04-23
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多