locked
How to resolve a hostname to an IP address in Windows Phone 8? RRS feed

  • Question

  • In my app,hostname like www.google.com will be entered.how to get ip address of that in Windows Phone 8?

    Please provide code snippet if possible

    Thanks in advance.

    Tuesday, June 25, 2013 12:03 PM

All replies

  • You can use class DeviceNetworkInformation. 

    Try this code:

    private void ResolveHostName(String hostName)
    {
        var endPoint = new DnsEndPoint(hostName, 80);
        Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.ResolveHostNameAsync(endPoint, OnNameResolved, null);
    }
    
    private void OnNameResolved(Microsoft.Phone.Net.NetworkInformation.NameResolutionResult result)
    {
        IPEndPoint[] endpoints = result.IPEndPoints;
        // Here your address if resolved
        if (endpoints != null && endpoints.Length > 0)
        {
            var ipAddress = endpoints[0].Address;
        }
    }
    and use it calling ResolveHostName("www.google.com");

    Tuesday, June 25, 2013 1:38 PM