المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : getting country ******** based on IP address [modified]



C# Programming
01-06-2010, 11:41 PM
Hey,

I am running into a weird problem. I am using below method to extract country ******** 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 = Get********(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 Get********(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 something in web.config?

modified on Tuesday, January 5, 2010 8:47 PM