By using this we can get current City and state.
Name spaces to Add:
----------------------
using System.Net;
using System.Text;
Code to Add:
--------------
string strCity = "", strState = "";
try
{
Response.Cache.SetNoStore();
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.AddHeader("Pragma", "No-Cache");// 'Tell proxy servers not to cache the page.
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
string IPADR = "";
if (Request.QueryString["IP"] != null)
IPADR = Request.QueryString["IP"].ToString();
else
IPADR = Request.ServerVariables["REMOTE_ADDR"];
WebClient objWebClient = new WebClient();
UTF8Encoding objUTF8 = new UTF8Encoding();
String strRequestState;
strRequestState = objUTF8.GetString(objWebClient.DownloadData("http://www.thewebhelp.com/tools/geoip/?ip_box=" + IPADR));
String strRequestCity;
strRequestCity = objUTF8.GetString(objWebClient.DownloadData("http://batchiplocator.webatu.com/api/locate.php?ip=" + IPADR));
string[] strCityName = GetClientCity(strRequestCity);
string[] strStateName = GetClientState(strRequestState);
if (strCityName.Length > 0)
{
if (strCityName[0] != null)
strCity = strCityName[0].ToString().TrimStart().TrimEnd();
}
if (strStateName.Length > 0)
{
if (strStateName[0] != null)
strState = strStateName[0].ToString().TrimStart().TrimEnd();
}
}
catch (Exception ex)
{
}
Methods to Add :
------------------
public static string[] GetClientCity(string strClientData)
{
int arrLength = 3;
string[] strData = new string[arrLength];
string strTotData = strClientData.Substring(strClientData.IndexOf("city", 0));
string[] strDetails = strTotData.Split(',');
if (strDetails.Length > 3)
{
if (strDetails[0].IndexOf("null") == -1)
{
string strShortData = strDetails[0].Replace("city", "").Substring(3);
int Length = strDetails[0].Replace("city", "").Substring(3).Length;
strData[0] = strShortData.Substring(0, Length - 1);
}
}
return strData;
}
public static string[] GetClientState(string strClientData)
{
int arrLength = 3;
string[] strData = new string[arrLength];
string strTotData = strClientData.Substring(strClientData.IndexOf("<td bgcolor=", 0));
string strShortData = strTotData.Substring(strTotData.IndexOf("<td bgcolor=", strTotData.IndexOf("</tr>", 0)));
string[] strDetails = strShortData.Split('\n');
if (strDetails.Length > 3)
{
strData[0] = Strip(strDetails[2]).TrimStart();
strData[1] = Strip(strDetails[3]).TrimStart();
}
return strData;
}