In ASP.NET, we can easily convert the Domain names to its related IP address like if a website like planetsourcecode.in has a domain name i.e planetsourcecode.com, It has also a corresponding Internet Protocol Address like 208.111.14.112.
this can be easily done in ASP.NET using C#
Steps for doing this:
1). First we have to include the namespace for this class
Default.aspx.cs
Using System.net;
2). Second include two textbox,one command button and Label as below
1--> txtDomain, textbox, to enter the domain names
2--> txtIPs, textbox, to get the desired IP address
3--> btnSubmit, button
4--> lblStatus,Label, for successful result
3). Double click on the command buttton and write the following in cod behind
DNSLookup(txtDomain.Text);
4). and write a method to do this work
protected void DNSLookup(string domain)
{
try
{
//performs the DNS lookup
IPHostEntry he = Dns.GetHostByName(domain);
IPAddress[] ip_addrs = he.AddressList;
txtIPs.Text = "";
foreach (IPAddress ip in ip_addrs)
{
txtIPs.Text += ip + "\n";
}
}
catch (System.Exception ex)
{
lblStatus.Text = ex.ToString();
}
}
Default.aspx
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Domain Name: | |
IP Address(es) | |
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
DNSLookup(txtDomain.Text);
}
protected void DNSLookup(string domain)
{
try
{
//performs the DNS lookup
IPHostEntry he = Dns.GetHostByName(domain);
IPAddress[] ip_addrs = he.AddressList;
txtIPs.Text = "";
foreach (IPAddress ip in ip_addrs)
{
txtIPs.Text += ip + "\n";
}
}
catch (System.Exception ex)
{
lblStatus.Text = ex.ToString();
}
}
}
About the author:
Planet Source Code is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development- 183 views