KBSoft IP Locator

Introduction

It is often necessary to get information about a user or a site by their IP address. For example, such a need appears at the determination of a site’s target audience. Having received the data about site users, it is possible to promote the site more effectively and develop different marketing actions. There are quite a lot of commercial services providing such information. The choice of such systems is often determined by the volume of information received, by its accuracy and importance, and also by the simplicity of adjusting and getting information. The purpose of this article is to show techniques of getting information about a user or a site by the IP address, and demonstrate an example of developing a demo application enabling you to get this information quickly and simply. You can find the demo application allowing you to receive information by IP address and the source code of this application in the zip file attached to the article. The demo application uses a simplified version of the class, delivered in the form of a DLL library. This class provides basic functions of information receipt by IP address. You can find the source code of this class here. You can use this code with research aims in your desktop or web applications for getting information about site users or with an aim to get information about some site (it is enough to know this site’s IP address).

This article is based on the experience of KBSoft, and information received from the documentation of IP addresses analyzing systems studied in this article.

Review of Services for Getting Information by IP Address

A lot of companies provide services for getting information by IP address. Some of them, that provide free demo services, are described in this article. One of them is CDYNE. This company presents a web service named IP2Geo, which returns information in the form of XML by IP address. This web service is convenient, and no restrictions were noticed for a test receipt of information. But, there are disadvantages as well: The service does not provide a large quantity of information and the accuracy of location definition is not very high.

The company, IP2Location, provides similar services. The company’s site provides more information than CDYNE service, but the demo page restricts the quantity of requests to not more than 20 per day and presents results in an inconvenient way for processing a form.

The company FraudLabs also uses the IP2Location database. Advantages and disadvantages coincide with those of the previous company with the exception of the fact that the demo page returns the result in XML form, which significantly simplifies the processing of this data. Moreover, with the help of demo pages it is possible to get much more information than that from the previous company.

The most convenient services, from the author’s point of view, are those provided by the company GeoBytes. Their advantage is a big volume of useful information, there is a possibility to get a reply from the server in XML form, and also more acceptable restrictions of the trial-version (not more than 20 requests per hour). Besides, for getting information in different formats, there is a possibility to use different templates, which can present information in a desirable view. Unfortunately, disadvantages also exist here: Requests are sent via HTTP GET, but not via web service (it is under development now); this influences the security of the sent confidential data not in the best form (in case you bought a paid access). Moreover, in case of the use of paid access for getting information, it is complicated due to the need to use a session token.

Why It’s Possible to Get Wrong Information

The most useful information that can be received by IP address is the user’s location. It is only necessary to get information about the latitude and the longitude for that. The rest of the information needed to make different reports (for example, monetary units, time zone, and so forth) can be received by knowing these characteristics. But, it’s not that simple to do as it seems at first sight. The IP address is not assigned with certain geographical location (for example, IP addresses of subnets are absolutely useless for determinating a geographical location). That is why it is impossible to get information about the location of the IP address possessor with an absolute accuracy. Furthermore, there are proxy-servers and NAT. These have an opaque structure; that is why it is impossible to receive the location of an IP address using proxy-server or NAT. What can be maximally received is the location of the proxy-server or NAT itself. Moreover, there are proxy-servers that generate random IP addresses or use the HTTP_X_FORWARDED_FOR header. IP address from mobile phones or satellite systems will distinguish themselves with a most inaccurate location. Nevertheless, in the majority of cases, the location of IP addresses possessors can be precisely determined.

Program Code Description

The designed KBS_IPLocatorClass class provides opportunities on the request of information from two sites: CDYNE and Geobytes. There is a possibility to request information about your own IP address and about any other IP address entered in the request field. Besides, the class presents the potential to parse the received XML data. Along with an XML template, Geobytes presents other templates providing the achievement of other aims as well (see here). The most useful template is valuepairsAll.txt, because this template presents the most complete information, but, unfortunately, problems can occur while parsing data received. Nevertheless, having developed a function on parsing and having added it into the class source code, you easily can add a possibility of processing any template and get information you are interested in with the help of the LookupCustomIP()class. Besides, the class’s flexible architecture enables you to add a request and process information from any site presenting such information.

Here is the LookupCustomIP() method code:

public bool LookupCustomIP(string ipAddress, ServerTypes serverType)
{
   try
   {
      //create the request URL
      string requestURL="";
      //add the host element of the URL
      switch (serverType)
      {
         case ServerTypes.Geobytes:
            requestURL = "http://www.geobytes.com/
                          IpLocator.htm?GetLocation&template="
               + strTemplateFileName + "&IpAddress=" + ipAddress;
            break;
         case ServerTypes.Cdyne:
            requestURL = "http://ws.cdyne.com/ip2geo/ip2geo.asmx/
                          ResolveIP?ipAddress="

               + ipAddress + "&licenseKey=0";
            break;
      }

      // create the web client and obtain the response data
      // as a byte array
      WebClient webclient = new WebClient();
      byte[] response =
         webclient.DownloadData(requestURL.ToString());
      // process the XML result to obtain a validation result
      return processXMLResult(response);

   }
   catch (Exception)
   {
      return false;
   }
}

Besides, you can get to know your real IP address by which you will be identified in the global network with the help of the demo application. The class also presents a possibility to check the validity of the entered IP. The source code, containing only basic functions necessary for getting information by IP, is attached to the article. In real projects developed by KBSoft, a class with wider opportunities that doesn’t use free services and allows users to collect information about visitors of the developed sites and make different reports is used.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read