Wednesday, April 25, 2012

Locating All details of IP Adress (Method 2)

In my previous post I have explained about how to locate the IP Address country it self. Please go through the before post : http://moretoc.blogspot.in/2012/04/locating-ipadress-using-aspnet-with-c.html. In Yhis post I am going to Explain about how to locate the whole address of the IP. To do this first download the htmlagilitypack from the following URL : http://htmlagilitypack.codeplex.com/releases/view/44954. Which will help us to read the html content.
After Downloading then create a new project and add the downloaded dll in to our solution.e; Right click on references and add the dll.

Then add the New Aspx page with the following code:


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        body
        {
        font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;  
        background-color: #ffffff;
        color: #4f6b72;     
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Panel ID="panelLoc" runat="server">
                    <asp:TextBox ID="txtIP" runat="server"></asp:TextBox>
                    <asp:Button ID="btnGetLoc"
                        runat="server" Text="Get IP Details" onclick="btnGetLoc_Click" />
                    <br />
                    <div id="kk" runat="server"></div>

                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
      
        <asp:UpdateProgress ID="updProgress"
        AssociatedUpdatePanelID="UpdatePanel1"
        runat="server">
            <ProgressTemplate>          
            <img alt="progress" src="images/progress.gif"/>       
            </ProgressTemplate>
        </asp:UpdateProgress>

    </div>
    </form>
</body>
</html>


After creating Aspx page then code behind and add the following namespaces.

using HtmlAgilityPack;


Then add the following code in the code behind file :


protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnGetLoc_Click(object sender, EventArgs e)
        {
            String url = String.Empty;

            if (txtIP.Text.Trim() != String.Empty)
            {
                url = String.Format("http://iplocationtools.com/ip_query2.php?ip={0}", txtIP.Text.Trim());
                HtmlWeb hw = new HtmlWeb();
                HtmlDocument doc = hw.Load(url);
                HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@align='center']/table[@cellspacing='0'][@width='650']");
                kk.InnerHtml = node.OuterHtml;
              
            }  
        }

Here I am sending the request to http://iplocationtools.com/ip_query2.php and downloading the data and then I am formating the output table in the Div kk.

Output:

 

No comments:

Post a Comment