How to get Region through IPAddress in Asp.Net



An IP address is a fascinating product of modern computer technology designed to allow one computer (or other digital device) to communicate with another via the Internet. IP addresses allow the location of literally billions of digital devices that are connected to the Internet to be pinpointed and differentiated from other devices. In the same sense that someone needs your mailing address to send you a letter, a remote computer needs your IP address to communicate with your computer.

An IP address consists of four numbers, each of which contains one to three digits, with a single dot (.) separating each number or set of digits. Each of the four numbers can range from 0 to 255.

Your IP address is something you probably rarely think about, but it's vitally important to your online lifestyle. Without an IP address, you wouldn't be able to get today's weather, check the latest news or look at videos online. So we can Store our client IPAddress we he/she visits our Site or Application.

In this Example we will how to get Region and Region Name using IPAddress.

HTML CODE:-
<div>
  <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
  <table cellpadding="0" cellspacing="0" width="50%" align="center" style="background-color: #f5f5f5; color: #4f6b72;">
   <tr>
    <td align="center">
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
       <asp:Panel ID="panelLoc" runat="server">
<asp:Label ID="Label2" runat="server" Text="Region and RegionName" Font-Bold="False" Font-Names="Monotype Corsiva" Font-Size="X-Large" Font-Underline="True"></asp:Label>
       <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Xml ID="Xml1" runat="server"></asp:Xml>
       </asp:Panel>
      </ContentTemplate>
     </asp:UpdatePanel>
     <asp:UpdateProgress ID="updProgress" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
      <ProgressTemplate>
       <img alt="progress" src="Image.gif" style="height: 31px; width: 49px" />
      </ProgressTemplate>
     </asp:UpdateProgress>
   </td>
  </tr>
 </table>
</div>

CODE BEHIND:-
public void bind()
{
  WebClient wc = new WebClient();
  Stream data = wc.OpenRead("http://ip-api.com/xml/");
  String str;
  using (StreamReader sr = new StreamReader(data))
  {
    string strReturnVal;
    str = sr.ReadToEnd();
    Console.WriteLine(@"Xml.Linq = obRoot.Elements(""query"")");
    XElement obRoot = XElement.Parse(str);
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(str);
    data.Close();
    XmlNodeList responseXML = xmldoc.GetElementsByTagName("query");
    NameValueCollection dataXML = new NameValueCollection();
dataXML.Add(responseXML.Item(0).ChildNodes[2].InnerText, responseXML.Item(0).ChildNodes[2].Value);
strReturnVal = responseXML.Item(0).ChildNodes[3].InnerText.ToString();//region
strReturnVal += "(" + responseXML.Item(0).ChildNodes[4].InnerText.ToString() + ")";//region name
Label1.Text = strReturnVal;
   }
}

Run your Web Application you web get region and region Code.  

Related Posts

Previous
Next Post »

Thanks for comments.....