بدست آوردن مشخصات دامنه از WhoIs در Asp.Net

دوشنبه 12 اسفند 1392

با استفاده از این مقاله میتونید مشخصات صاحب دامنه و اطلاعات کامل مربوط به دامنه رو در Asp.Net بدست بیارید

بدست آوردن مشخصات دامنه از WhoIs در Asp.Net

سلام دوستان

با استفاده از این مقاله میتونید مشخصات صاحب دامین و غیره رو بدست بیارید

یا چک کنید دامنه موجود است یا خیر

 

در پروژه یک کلاسی وجود داره با نام Whois.cs که تمام این کارها را انجام خواهد داد

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Whois
{
	public delegate void WhoisEventHandler(object sender, WhoisEventArgs e);
	/// <summary>
	/// Our class containing information to be passed to
	/// the lookup event.
	/// </summary>
	public class WhoisEventArgs : EventArgs 
	{
		public string WhoisInfo
		{
			get
			{
				return this.whoisInfo;
			}
		}
		public string WhoisServer
		{
			get
			{
				return this.whoisServer;
			}
		}
		private string whoisInfo;
		private string whoisServer;
		public WhoisEventArgs(string Info,string Server)
		{
			this.whoisInfo = Info;
			this.whoisServer = Server;
		}
	}
	/// <summary>
	/// Perform a whois lookup on a domain name, enabling you to 
	/// find out if a domain name is registered, and if it is - 
	/// to whom and what name servers it uses.
	/// </summary>
	public class Whois
	{
		/// <summary>
		/// The server used to perform the whois. If this is not set,
		/// then an attempt to find the whois server automatically is
		/// made. If none is found, the internic whois server is used
		/// by default.
		/// </summary>
		public string WhoisServer
		{
			get
			{
				return this.whoisServer;
			}
			set
			{
				this.whoisServer = value;
			}
		}
		/// <summary>
		/// Called when the lookup has been complete.
		/// </summary>
		public event WhoisEventHandler LookupComplete;
		private string whoisServer = "";
		/// <summary>
		/// Performs a whois lookup on the domain provided.
		/// </summary>
		/// <param name="Domain">The domain to lookup.</param>
		/// <returns>Information returned by the domain whois server,
		/// which can then be string parsed to see if the domain is available or taken.</returns>
		public void Lookup(string Domain)
		{
			string result = "";
			string[] parts = new string[] {};
			// Knock off http and www if it's in the Domain
			if ( Domain.StartsWith("http://") )
			{
				Domain = Domain.Replace("http://","");
			}
			if ( Domain.StartsWith("www.") )
			{
				Domain = Domain.Substring(4,Domain.Length -4);
			}
			
			if ( Domain.IndexOf(".tv") != -1 || Domain.IndexOf(".pro") != -1 || Domain.IndexOf(".name") != -1)
			{
				// As result says - certain domain authorities like to keep their whois service private.
				// There maybe extra tlds to add.
				result = "'.pro','.name', and '.tv' domains require an account for a whois";
			}
			else
			{
				if ( Domain.IndexOf(".") != -1 )
				{
					// Find the whois server for the domain ourselves, if non set.
					if ( this.whoisServer == "" )
					{
						this.whoisServer = this.getWhoisServer(Domain);
					}
					// Connect to the whois server
					TcpClient tcpClient = new TcpClient();
					tcpClient.Connect(this.whoisServer,43);
					NetworkStream networkStream  = tcpClient.GetStream();
					// Send the domain name to the whois server
					byte[] buffer = ASCIIEncoding.ASCII.GetBytes(Domain + "\r\n");
					networkStream.Write(buffer,0,buffer.Length);
					// Read back the results
					buffer = new byte[8192];
					int i = networkStream.Read(buffer,0,buffer.Length);
					while ( i > 0)
					{
						i = networkStream.Read(buffer,0,buffer.Length);
						result += ASCIIEncoding.ASCII.GetString(buffer);;
					}
					networkStream.Close();
					tcpClient.Close();
				}
				else
				{	
					result = "Please enter a valid domain name.";
				}
			}
			// Fire event with the info of the lookup
			if ( this.LookupComplete != null )
			{
				WhoisEventArgs whoisEventArgs = new WhoisEventArgs(result,this.whoisServer);
				this.LookupComplete(this,whoisEventArgs);
			}
		}
		/// <summary>
		/// Gets the whois server for a domain using
		/// whois-servers.net.
		/// </summary>
		/// <param name="domainName">The domain name to retrieve the whois server for.</param>
		/// <returns>The whois server hostname.</returns>
		private string getWhoisServer(string domainName)
		{
			string [] parts = domainName.Split('.');
			string tld = parts[parts.Length -1];
			string host = tld + ".whois-servers.net";
			// .tk doesn't resolve, but it's whois server is public
			if ( tld == "tk")
			{
				return "whois.dot.tk";
			}
			try
			{
				IPHostEntry ipHostEntry = Dns.GetHostByName(host);
				if ( ipHostEntry.HostName ==  host )
				{
					// No entry found, use internic as default
					host = "whois.internic.net";
				}
				else
				{
					host = ipHostEntry.HostName;
				}
				return host;
			}
			catch
			{
				host = "whois.internic.net";
				return host;
			}
		}
	}
}

 

بقیه کدها در فرم

       Whois.Whois w=new Whois.Whois();
       w.LookupComplete += w_LookupComplete;
            w.Lookup(TextBox1.Text);

 

موفق و پیروز باشید

نمونه ضمیمه شده است

فایل های ضمیمه

ایمان مدائنی

نویسنده 1299 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید