我需要从一个DnsEndPoint获取给定主机名的IP地址,并将其转换为IPEndPoint。 我该如何做?WP7缺少Dns.GetHostEntry函数,所以有没有方法可以不创建Socket,发送数据到主机,然后从主机接收ping并读取RemoteEndPoint属性来获取主机的IP地址?
我需要从一个DnsEndPoint获取给定主机名的IP地址,并将其转换为IPEndPoint。 我该如何做?WP7缺少Dns.GetHostEntry函数,所以有没有方法可以不创建Socket,发送数据到主机,然后从主机接收ping并读取RemoteEndPoint属性来获取主机的IP地址?
Microsoft.Phone.Net.NetworkInformation
命名空间中的 DeviceNetworkInformation.ResolveHostNameAsync
方法,如下所示:public void DnsLookup(string hostname)
{
var endpoint = new DnsEndPoint(hostname, 0);
DeviceNetworkInformation.ResolveHostNameAsync(endpoint, OnNameResolved, null);
}
private void OnNameResolved(NameResolutionResult result)
{
IPEndPoint[] endpoints = result.IPEndPoints;
// Do something with your endpoints
}
http://msdn.microsoft.com/en-us/library/system.net.dns.aspx
在我的应用程序开始时,我将创建一个Web服务调用到主机(其中包含Web服务器),请求IP地址。我认为我会在此期间解决这个问题。[ServiceContract]
public interface IService1
{
[OperationContract]
string GetIpAddress(string value);
}
public class Service1 : IService1
{
public string GetIpAddress()
{
// Add the proper error handling and collection matching of course
IPAddress s = Dns.GetHostAddresses("www.mysite.com")[0];
return s.ToString();
}
}
框架中没有内置此功能的方法。假设主机支持ping,您可以使用套接字。这将取决于您运行的网络(我认为您无法控制此内容)和应用程序的确切要求。
如果您只有IP地址,通过IP地址让您的应用程序工作并不需要主机名可能更容易。