Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Using IP to Country Web Service Within .NET Application

, 29 Jun 2010
Rate this:
Please Sign up or sign in to vote.
This article explains how to consume a free IP2Country web service within a .NET application.

Introduction

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.

Fig1.png

Figure 1

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#.

/////////////////////////// 
///WSTest.aspx/// 
/////////////////////////// 
<%@ Page Language="C#" AutoEventWireup="true" 
	CodeBehind="WSTest.aspx.cs" Inherits="_9KBuild.WSClient.WSTest" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
<title>IP To Country Web Service Test Page</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div style="padding-bottom:10px;"> 
Enter IP:&nbsp;<asp:TextBox ID="tbIpAddress" runat="server"></asp:TextBox> 
<asp:Label ID="lbMsg" runat="server" Text=""></asp:Label> 
</div> 
<div> 
Country Code:&nbsp;<asp:Label ID="lbCountryCode" runat="server" 
	Style="font-weight:bold; color:#FF0000" Text=""></asp:Label> 
</div> 
<p> 
<asp:Button ID="btnFindCountry" runat="server" Text="Find Country" 
	onclick="btnFindCountry_Click" Width="125px" /> 
</p> 
</form> 
</body> 
</html> 
/////////////////////////////// 
///WSTest.aspx.cs/// 
/////////////////////////////// 
using System; 
using System.Data; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Text.RegularExpressions; 
using NINEK.WS; 
namespace _9KBuild.WSClient 
{ 
/// <summary> 
/// This method call will get two-character country code based on ISO 3166. 
/// Web Service @ http://www.9kgames.com/WS/WSIP2Country.asmx 
/// Demo page @ http://www.9kgames.com/WSClient/IP_To_Country.aspx 
/// eSupersun's Blog: http://esupersun.blogspot.com/ 
/// </summary> 
/// 
/// 
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!"; 
tbIpAddress.Focus(); 
return; 
} 
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.

Fig2.png

Figure 2

Demo Page

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.

Fig3.png

Figure 3

History

  • 29th June, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

esupersun
Team Leader MICBOTS
Canada Canada
Fred Yang is the lead developer at Micbots (micbots.com), a Vancouver based game studio that produces fun and attractive mobile games on market for all to enjoy. So far the released titles include Sokoban Deluxe, Lost Warrior, Crocodile Island, Ozman Quest, Temple of Arceus, and Nezeh Series.
As an active member of the game industry, he has contributed numerous brilliant articles to Apphex Forums (apphex.com), a top mobile game developers forum, covering comprehensive technical discussions and cutting-edge programming tutorials on Android/iOS game development.
Fred holds a Ph.D. in Applied Mathematics and is also an inspiring mentor for aspiring programmers and artists trying to tap into the game industry.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralGetCountryCode method was deprecated, pls use GetCountryCodeAuth instead. Pinmemberesupersun2-Apr-11 6:19 
GeneralGetCountryCode will be deprecated soon, pls use GetCountryCodeAuth instead. Pinmemberesupersun28-Mar-11 20:28 
Generalerror in using NINEK.WS; Pinmembersuhasrao30-Aug-10 9:42 
Sir i am getting error in using NINEK.WS; I also add web reference but still i am getting error. pls help me.
GeneralRe: error in using NINEK.WS; Pinmemberesupersun31-Aug-10 5:46 
GeneralGreat job! Thanks for sharing this free web service. Pinmembermsindhwani2-Jul-10 14:06 
GeneralMy vote of 5 Pinmembermsindhwani2-Jul-10 14:04 
QuestionFormat? PinsubeditorWalt Fair, Jr.29-Jun-10 12:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 29 Jun 2010
Article Copyright 2010 by esupersun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid