IP Address to Country/State (for CommunityServer) using www.hostip.info

I am about to begin overhauling a pretty big site (DTS)
and I’ve been collecting user resolution data to determine if we can do
a 1024×768 layout. While browsing for IP-to-country databases, I
stumbled upon http://www.hostip.info/,
a site which keeps up-to-date data of IP addresses and their country
codes and state information. They offer a free download of the database
(about 25 MB, 500 MB unzipped!) as well as a simple GET page where you
can retrieve the info programmatically.  I wrote a quick ASP.NET
sample script to pull the data:

string ipUrl = "http://www.hostip.info/api/get.html?ip=" + Request.UserHostAddress;

WebClient webClient = new WebClient();
Byte[] ipInfoBytes = webClient.DownloadData(ipUrl);
UTF8Encoding encoding = new UTF8Encoding();
string ipInfo = encoding.GetString(ipInfoBytes);

Response.Write(ipInfo);

Long term, it’d probably be better to download the data and use your
own local copy, but their response time was pretty zippy and it works
for now.

Then I thought it would be cool to hook it up to CommunityServer‘s new CSModule
event model and pull user’s locations when a new account is created so
that the Location property is automatically filled in. This could also
be done every time a user logged in (the UserValidated event) which
would make things very interesting!

Here’s a link to the CSModule code:

If you want to use it out of the box, download the second file, compile it, and
copy RefreshDevelopment.CommunityServer.Modules.UserIpLocation.dll into
your /bin/ folder. Then add the following line to your
CommunityServer.config file between the tag.


	

1 thought on “IP Address to Country/State (for CommunityServer) using www.hostip.info

  1. Thank u so much.
    its really very good for me
    I m very Happy because we try much-much but not geting exact value but now I have value as I want.
    Thanks a lot
    Dilip Kumar

Comments are closed.