getting country location based on IP address [modified]
-
Hey, I am running into a weird problem. I am using below method to extract country location based on IP address.
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
//To get the IP address of the machine and not the proxy
m_IPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
m_IPAddress = HttpContext.Current.Request.UserHostAddress;
}
DataTable dt = GetLocation(m_IPAddress);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
m_IPCountry = dt.Rows[0]["City"].ToString() + "," + dt.Rows[0]["RegionName"].ToString() + "," + dt.Rows[0]["CountryName"].ToString() + "," + dt.Rows[0]["CountryCode"].ToString();
}
else
{
}
}
private DataTable GetLocation(string ipaddress)
{//Create a WebRequest WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + ipaddress); //Create a Proxy WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true); //Assign the proxy to the WebRequest rssReq.Proxy = px; //Set the timeout in Seconds for the WebRequest rssReq.Timeout = 2000; try { //Get the WebResponse WebResponse rep = rssReq.GetResponse(); //Read the Response in a XMLTextReader XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream()); //Create a new DataSet DataSet ds = new DataSet(); //Read the Response into the DataSet ds.ReadXml(xtr); return ds.Tables\[0\]; } catch { return null; } }
when i try to catch the exception i get error msg "The remote server returned an error: (503) Server Unavailable." the website is hosted in australia. I am logging all the details in database. IP address is correct at all time just after midnight in australia i am able to get country information while during the day it becomes null. any idea what am i doing wrong. probably need to add
-
Hey, I am running into a weird problem. I am using below method to extract country location based on IP address.
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
//To get the IP address of the machine and not the proxy
m_IPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
m_IPAddress = HttpContext.Current.Request.UserHostAddress;
}
DataTable dt = GetLocation(m_IPAddress);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
m_IPCountry = dt.Rows[0]["City"].ToString() + "," + dt.Rows[0]["RegionName"].ToString() + "," + dt.Rows[0]["CountryName"].ToString() + "," + dt.Rows[0]["CountryCode"].ToString();
}
else
{
}
}
private DataTable GetLocation(string ipaddress)
{//Create a WebRequest WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + ipaddress); //Create a Proxy WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true); //Assign the proxy to the WebRequest rssReq.Proxy = px; //Set the timeout in Seconds for the WebRequest rssReq.Timeout = 2000; try { //Get the WebResponse WebResponse rep = rssReq.GetResponse(); //Read the Response in a XMLTextReader XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream()); //Create a new DataSet DataSet ds = new DataSet(); //Read the Response into the DataSet ds.ReadXml(xtr); return ds.Tables\[0\]; } catch { return null; } }
when i try to catch the exception i get error msg "The remote server returned an error: (503) Server Unavailable." the website is hosted in australia. I am logging all the details in database. IP address is correct at all time just after midnight in australia i am able to get country information while during the day it becomes null. any idea what am i doing wrong. probably need to add
I cut and pasted your url [http://freegeoip.appspot.com] into a browser and received :- "App Engine Error Over Quota This Google App Engine application is temporarily over its serving quota. Please try again later. " so is there a possibility its being hammered at some stage of the day and when you want to access it its already 'over quota' ? 'g'
-
I cut and pasted your url [http://freegeoip.appspot.com] into a browser and received :- "App Engine Error Over Quota This Google App Engine application is temporarily over its serving quota. Please try again later. " so is there a possibility its being hammered at some stage of the day and when you want to access it its already 'over quota' ? 'g'
-
undoubtedly there are other services 'out there' or other databases - wether they are free or not, remains to be seen 'g'
-
see also things like ... Optimized IP to ISO3166 Country Code Mapping in C#[^] 'g'
-
see also things like ... Optimized IP to ISO3166 Country Code Mapping in C#[^] 'g'
I tried to implement this solution http://dotnetguts.blogspot.com/2008/06/finding-country-from-visitors-ip-in.html[^] i didnt use the last step to import database as I only need a country code not flag but its not working. since in the example its using a aspx page to display but in my case I have a all the page which is derived from basepage.cs in basepage.cs I am trying to get a countrycode and store in a database as a pageview record. could you please help as to why its null. I am not missed any of the steps mentioned in the article. Thank you
-
I tried to implement this solution http://dotnetguts.blogspot.com/2008/06/finding-country-from-visitors-ip-in.html[^] i didnt use the last step to import database as I only need a country code not flag but its not working. since in the example its using a aspx page to display but in my case I have a all the page which is derived from basepage.cs in basepage.cs I am trying to get a countrycode and store in a database as a pageview record. could you please help as to why its null. I am not missed any of the steps mentioned in the article. Thank you
-
ok this one was easy. http://www.eggheadcafe.com/community/aspnet/7/10061470/how-to-get-country-and-ci.aspx[^]
actually, all the free web services seems to reject request after it reaches certain daily hits. which in my case is true. if google crawls my site i am using the service to get the country code so i gave up on using free webservice to get a country code based on IP. I used this one http://dotnetguts.blogspot.com/2008/06/finding-country-from-visitors-ip-in.html[^] but its getting a wrong country codes for example in below. I am using my iphone which is in sydney, australia, its logging country code as FR. could someone please help? 58.104.249.183 FR Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16
-
Hey, I am running into a weird problem. I am using below method to extract country location based on IP address.
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
//To get the IP address of the machine and not the proxy
m_IPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
m_IPAddress = HttpContext.Current.Request.UserHostAddress;
}
DataTable dt = GetLocation(m_IPAddress);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
m_IPCountry = dt.Rows[0]["City"].ToString() + "," + dt.Rows[0]["RegionName"].ToString() + "," + dt.Rows[0]["CountryName"].ToString() + "," + dt.Rows[0]["CountryCode"].ToString();
}
else
{
}
}
private DataTable GetLocation(string ipaddress)
{//Create a WebRequest WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + ipaddress); //Create a Proxy WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true); //Assign the proxy to the WebRequest rssReq.Proxy = px; //Set the timeout in Seconds for the WebRequest rssReq.Timeout = 2000; try { //Get the WebResponse WebResponse rep = rssReq.GetResponse(); //Read the Response in a XMLTextReader XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream()); //Create a new DataSet DataSet ds = new DataSet(); //Read the Response into the DataSet ds.ReadXml(xtr); return ds.Tables\[0\]; } catch { return null; } }
when i try to catch the exception i get error msg "The remote server returned an error: (503) Server Unavailable." the website is hosted in australia. I am logging all the details in database. IP address is correct at all time just after midnight in australia i am able to get country information while during the day it becomes null. any idea what am i doing wrong. probably need to add
good question. 5! some one has a solution here. Another solution here.
♫ 99 little bugs in the code, 99 bugs in the code We fix a bug, compile it again 101 little bugs in the code ♫