This tutorial will show you how to perform a DNS query using the .NET System.Net class, ASP.NET 2.0 and VB.NET
The .NET Framework offers a number of types that makes accessing networked resources easy.
To perform a DNS query, we will need to first import the System.Net namespace. The System.Net namespace contains the Dns.GetHostByName method, IPHostEntry container class and IPAddress class we will use to perform our DNS query.
| imports System.Net; |
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
We'll put our code in a subroutine called DNSLookup() and call it from the btnSubmit_Click() event.
When the btnSubmit_Click() event fires it calls our DNSLookup() function. This function creates a new instance of the IPHostEntry container class and assigns it a host entry returned from the Dns.GetHostByName method. It then create an array of IPAddress objects using the AddressList of our host entry object. After that, it iterates through each ip and adds it to the text box.
| Protected Sub DNSLookup(ByVal domain As String) Try End Sub 'performs the DNS lookup Catch ex As System.ExceptionDim he As IPHostEntry = Dns.GetHostEntry(domain) Dim ip_addrs As IPAddress() = he.AddressList txtIPs.Text = "" For Each ip As IPAddress In ip_addrs txtIPs.Text = ip.ToString() Next ip lblStatus.Text = ex.ToString() End Try |
| <table> <tr> </table> <td align="right" bgcolor="#eeeeee" class="header1">Domain Name:</td> </tr><td bgcolor="#FFFFFF"><asp:textbox ID="txtDomain" runat="server"></asp:textbox><br /> <asp:button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /></td> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1">IP Address(es) </tr>Assigned to Domain Name:</td> <td bgcolor="#FFFFFF"> <br /> <asp:textbox ID="txtIPs" runat="server"></asp:textbox><br /> <br /> <asp:label ID="lblStatus" runat="server"></asp:label></td> |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The flow for the code behind page is as follows:| Imports System.Net Partial Public Class _Default : Inherits System.Web.UI.Page Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) End ClassDNSLookup(txtDomain.Text) End SubProtected Sub DNSLookup(ByVal domain As String) Try End Sub'performs the DNS lookup Catch ex As System.ExceptionDim he As IPHostEntry = Dns.GetHostEntry(domain) Dim ip_addrs As IPAddress() = he.AddressList txtIPs.Text = "" For Each ip As IPAddress In ip_addrs txtIPs.Text = ip.ToString() Next ip lblStatus.Text = ex.ToString() End Try |
No comments:
Post a Comment