Click here to Skip to main content
13,143,985 members (33,264 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


35 bookmarked
Posted 29 Jun 2010

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.


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.

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


Figure 1

Enter 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 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="" > 
<head runat="server"> 
<title>IP To Country Web Service Test Page</title> 
<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> 
Country Code:&nbsp;<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" /> 
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 @ 
/// Demo page @ 
/// eSupersun's Blog: 
/// </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!"; 
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.


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 and then click on “IP Viewer” on the bar menu or go to 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 @


Figure 3


  • 29th June, 2010: Initial post


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


About the Author

Canada Canada
Fred Yang is a mentor for aspiring programmers and artists who are interested in breaking into the game industry.

You may also be interested in...


Comments and Discussions

GeneralGetCountryCode method was deprecated, pls use GetCountryCodeAuth instead. Pin
esupersun2-Apr-11 6:19
memberesupersun2-Apr-11 6:19 
GeneralGetCountryCode will be deprecated soon, pls use GetCountryCodeAuth instead. Pin
esupersun28-Mar-11 20:28
memberesupersun28-Mar-11 20:28 
Generalerror in using NINEK.WS; Pin
suhasrao30-Aug-10 9:42
membersuhasrao30-Aug-10 9:42 
GeneralRe: error in using NINEK.WS; Pin
esupersun31-Aug-10 5:46
memberesupersun31-Aug-10 5:46 
GeneralGreat job! Thanks for sharing this free web service. Pin
msindhwani2-Jul-10 14:06
membermsindhwani2-Jul-10 14:06 
GeneralMy vote of 5 Pin
msindhwani2-Jul-10 14:04
membermsindhwani2-Jul-10 14:04 
QuestionFormat? Pin
Walt Fair, Jr.29-Jun-10 12:50
subeditorWalt Fair, Jr.29-Jun-10 12:50 
Please check your formatting. Source code should be enclosed between <pre> and </pre> tags, otherwise it's almost unreadable and very confusing.

Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 29 Jun 2010
Article Copyright 2010 by esupersun
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid