4AspNet IPInfo v1.0

IPInfo.GetInfoByHostAddress Method (IPAddress)

Retrieves country information by an IP address.

public CountryInfo GetInfoByHostAddress(
   IPAddress address
);

Parameters

address
The IP address represented by a IPAddress object.

Return Value

The CountryInfo structure.

Remarks

Use this overloaded version of GetInfoByHostAddress method when you need to specify an IPAddress object as an argument.

When country information for the specified address is absent and ThrowIfNotFound property is set to false method will return CountryInfo structure without significant information. If ThrowIfNotFound is set to true, the exception of type CountryInfoNotFoundException will be thrown.

Exceptions

Exception TypeCondition
ArgumentNullExceptionThrown when specified argument is a null reference (Nothing in Visual Basic).
CountryInfoNotFoundExceptionThrown when property ThrowIfNotFound is set to true and there is no country information for a specified address.

Example

The following example demonstrates using of GetInfoByHostAddress specifying the IPAddress object as an argument.

using System;
using System.Net;
using ForAspNet.IPInfo;

namespace Test
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            License.LicenseKey = "<put license string here>";
        
            CountryInfo[] infos =  GetMultipleInfoForHostName("www.microsoft.com");
        }

        static CountryInfo[] GetMultipleInfoForHostName(string hostName)
        {
            // get server related information.
            IPHostEntry heserver = Dns.Resolve(hostName);

            // creating an array of CountryInfo structures with size enough to hold
            // information for all resolved IP entries
            CountryInfo[] ret = new CountryInfo[heserver.AddressList.Length];

            // initializing a new instance of IPInfo
            IPInfo ipi = new IPInfo();

            // iterate through address list and retrieve country information for each entry
            for(int i=0; i<heserver.AddressList.Length; i++)
                ret[i] = ipi.GetInfoByHostAddress(heserver.AddressList[i]);

            return ret;
        }
    }
}

See Also

IPInfo Class | ForAspNet.IPInfo Namespace | IPInfo.GetInfoByHostAddress Overload List