Get User’s Country Name Using IP Address


How can get the user’s country name using IP address in Classic ASP and Javascript?

http://www.webservicex.net provides web services for get the country name based on ip address.

In ASP using javascript we can achieve the task.

Step1 : Refer the webservice using SOAPClient object

var strUrlForwebservicex = http://www.webservicex.net/geoipservice.asmx?wsdl

var objSoapClient = Server.CreateObject(‘MSSOAP.SoapClient30’)

objSoapClient.ClientProperty(“ServerHTTPRequest”) = true;

objSoapClient.MSSoapInit(strUrlForwebservicex);

Step2 : Get user’s IP address

var strIPAddress = Request.ServerVariables(“REMOTE_ADDR”);

Step3 : Pass the parametes on GetGeoIP webmethod

var strCountryName =objSoapClient.GetGeoIP(strIPAddress);

Complete Script :

<%

var strUrlForwebservicex = “http://www.webservicex.net/geoipservice.asmx?wsdl”
var objSoapClient = Server.CreateObject(‘MSSOAP.SoapClient30’)
objSoapClient.ClientProperty(“ServerHTTPRequest”) = true;
try
  {
objSoapClient.MSSoapInit(strUrlForwebservicex);
objSoapClient.ConnectorProperty(“ConnectTimeout”) = 600000;
var strIPAddress = Request.ServerVariables(“REMOTE_ADDR”);
var strCountryName =objSoapClient.GetGeoIP(strIPAddress);
if (strCountryName == “RESERVED”)
{
   strCountryName = “”;
}
}
catch (e)
{
strCountryName = “”;

Response.Write(“Following error messages occured : ” + e.message)
}

%>