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