Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. getting country location based on IP address [modified]

getting country location based on IP address [modified]

Scheduled Pinned Locked Moved C#
helpdatabasecomsysadminxml
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uglyeyes
    wrote on last edited by
    #1

    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

    G R 2 Replies Last reply
    0
    • U uglyeyes

      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

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      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'

      U 1 Reply Last reply
      0
      • G Garth J Lancaster

        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'

        U Offline
        U Offline
        uglyeyes
        wrote on last edited by
        #3

        is there a work around to get a country details based on IP without using this service?

        G 2 Replies Last reply
        0
        • U uglyeyes

          is there a work around to get a country details based on IP without using this service?

          G Offline
          G Offline
          Garth J Lancaster
          wrote on last edited by
          #4

          undoubtedly there are other services 'out there' or other databases - wether they are free or not, remains to be seen 'g'

          1 Reply Last reply
          0
          • U uglyeyes

            is there a work around to get a country details based on IP without using this service?

            G Offline
            G Offline
            Garth J Lancaster
            wrote on last edited by
            #5

            see also things like ... Optimized IP to ISO3166 Country Code Mapping in C#[^] 'g'

            U 1 Reply Last reply
            0
            • G Garth J Lancaster

              see also things like ... Optimized IP to ISO3166 Country Code Mapping in C#[^] 'g'

              U Offline
              U Offline
              uglyeyes
              wrote on last edited by
              #6

              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

              U 1 Reply Last reply
              0
              • U uglyeyes

                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

                U Offline
                U Offline
                uglyeyes
                wrote on last edited by
                #7

                ok this one was easy. http://www.eggheadcafe.com/community/aspnet/7/10061470/how-to-get-country-and-ci.aspx[^]

                U 1 Reply Last reply
                0
                • U uglyeyes

                  ok this one was easy. http://www.eggheadcafe.com/community/aspnet/7/10061470/how-to-get-country-and-ci.aspx[^]

                  U Offline
                  U Offline
                  uglyeyes
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • U uglyeyes

                    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

                    R Offline
                    R Offline
                    Ravi Sant
                    wrote on last edited by
                    #9

                    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 ♫

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups