In some circumstances, we may need to determine the country code by a visiting an external IP address via .NET technologies. There’re quite a few articles discussing this on the websites. I tried the method in the article below, however, looks like it takes a long time (several seconds) to get the country code based on a given IP address. http://dotnetguts.blogspot.com/2008/06/finding-country-from-visitors-ip-in.html.
In this article, I will introduce a web service named
WSIP2Country which can determine the Country by IP address in real time. It's lightning fast and free for everyone. More importantly, its Country to IP database has over 100K IP records and 243 countries as well as is updated on a regularly basis. You will find how easy it is to utilize this external Web service on your .NET side as mentioned.
About Web Service
As defined in Wikipedia.org, Web services are typically application programming interfaces (API) or web APIs that are accessed via Hypertext Transfer Protocol and executed on a remote system hosting the requested services. The Web service eases the process of integrating the two disparate environments, so it was a win-win for both sides. Using a Web service in a .NET project involves creating and compiling a proxy class, which is generated from a reference to the Web service. This reference may be in several formats:
- o ASMX file: A Web service created with the .NET Framework
- o WSDL file: A file or URL that includes the Web Services Description Language (WSDL), which describes the service and its interface
- o DISCO: A Microsoft technology for publishing and discovering Web services (It makes it possible for clients to reflect against endpoints to discover services and their associated WSDL documents.)
If you use Visual Studio .NET, it's easy to add a Web service to your project by adding a Web Reference. Go into the Solution Explorer and select your project, then right click on the References item and select Add Web Reference. When you do this, a dialog as shown in Figure 1 pops up. The dialog allows you enter a URL to locate the Web Service you want to create a reference for.
Enter http://9kgames.com/WS/WSIP2Country.asmx?WSDL in the URL textbox above and press “Go” button, a message “1 Service Found: WS2IP2Country” will show up within the field of “Web services found at this URL”.
The Web service
WSIP2Country exposes a web method named
GetCountryCode which accepts the IP address parameter as a string value, while returning a 2-character country code based on ISO 3166. You can go http://9kgames.com/WS/WSIP2Country.asmx for its description.
Consuming the WSIP2Country Web Service
At this point, you can use the Web service in a project. I put forward a simple test page here via ASP.NET/C#.
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="WSTest.aspx.cs" Inherits="_9KBuild.WSClient.WSTest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
<title>IP To Country Web Service Test Page</title>
<form id="form1" runat="server">
Enter IP: <asp:TextBox ID="tbIpAddress" runat="server"></asp:TextBox>
<asp:Label ID="lbMsg" runat="server" Text=""></asp:Label>
Country Code: <asp:Label ID="lbCountryCode" runat="server"
Style="font-weight:bold; color:#FF0000" Text=""></asp:Label>
<asp:Button ID="btnFindCountry" runat="server" Text="Find Country"
onclick="btnFindCountry_Click" Width="125px" />
public partial class WSTest : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
lbMsg.Text = string.Empty;
protected void btnFindCountry_Click(object sender, EventArgs e)
if (tbIpAddress.Text.Equals("") || !IsValidIP(tbIpAddress.Text))
lbMsg.Text = "Please enter a valid IP Address!";
WSIP2Country oService = new WSIP2Country();
lbCountryCode.Text = oService.GetCountryCode(tbIpAddress.Text);
On the test page, enter an IP address and then press “Find Country” button, the 2-character country code will display as shown in Figure 2.
Consuming a Web service in a .NET application is a straightforward process. As Web services continue to evolve and develop, you may find a need for them in your next project.
To visit the demo page, you can login at http://9kgames.com and then click on “IP Viewer” on the bar menu or go to http://9kgames.com/IP_To_Country.aspx directly. This demo page will show the country name and its flag based on the IP address entered (Figure 3). For more information, please visit eSupersun’s blog @ http://esupersun.blogspot.com.
- 29th June, 2010: Initial post