Veysel Uğur KIZMAZ

C# ile WhoIs Sorgulama

16.06.2012Okunma Sayısı: 6827Kategori: C#.Net

Merhaba arkadaslar,

Bu makalemizde C# ile bir web sayfasinin alan adi bilgilerine ulasacagiz.

Projeyi WindowsApplication olarak yapacagiz.

Simdi asagidaki gibi tasarim ekranimizi olusturalim:

Formdaki TextBox'larin isimlerini asagidaki gibi belirledim, kod içerisinde de bu isimleri kullandim:

NS1 -> tbWns1
NS2 -> tbWns2
Açilis Tarihi -> tbWacilisTarihi
Güncelleme Tarihi -> tbWguncellemeTarihi
Bitis Tarihi -> tbWbitisTarihi

Uygulamamiz "tr" uzantili alan adlari için "whois.nic.tr" üzerinden, diger alan adlari için "whois.internic.com" üzerinden sorgulama yapmaktadir.

Uygulamamizin kodlarinin çalisabilmesi için asagidaki namespace'leri eklemeyi unutmayalim:

using System.Net.Sockets;
using System.IO;
 
    private void Sorgula()
        {
            string whoisSonucu = string.Empty;
            TcpClient client = new TcpClient();
            string[] tarihim = new string[3];
            string[] ns = new string[2];
            string[] date = new string[3];
            StreamReader oku;
            Stream str;
            byte[] diziDomain;
            string domain;
            string satir;
            if (textBox1.Text.EndsWith(".tr"))
            {
                try
                {
                    client.Connect("whois.nic.tr", 43);
                    domain = textBox1.Text + System.Environment.NewLine;
                    diziDomain = Encoding.ASCII.GetBytes(domain.ToCharArray());
                    str = client.GetStream();
                    str.Write(diziDomain, 0, domain.Length);
                    oku = new StreamReader(client.GetStream(), Encoding.ASCII);
 
                    //Gelen veriyi satir satir okuyup islem yap
                    while ((satir = oku.ReadLine()) != null)
                    {
                        //NameServer'lari bul
                        if (satir.Trim().StartsWith("** Domain Servers"))
                        {
                            satir = oku.ReadLine();
                            ns[0] = satir.Trim();
                            satir = oku.ReadLine();
                            ns[1] = satir.Trim();
                        }
                        if (satir.Trim().StartsWith("Created on"))
                        {
                            date[0] = "";
                            date[1] = satir.Substring(satir.IndexOf(':') + 1).Trim();
                            satir = oku.ReadLine();
                            date[2] = satir.Substring(satir.IndexOf(':') + 1).Trim();
 
                        }
                    }
                    tbWns1.Text = ns[0];
                    tbWns2.Text = ns[1];
 
                    //Ay bilgisini al
                    foreach (string tarih in date)
                    {
                        if (tarih != string.Empty)
                        {
                            string ay = tarih.Substring(tarih.IndexOf('-') + 1, ((tarih.LastIndexOf('-')) - (tarih.IndexOf('-'))) - 1);
                        }
                    }
 
                    //Ay bilgisini Türkçelestir
                    string ayim;
                    int counttarihim = 0;
                    foreach (string tarih in date)
                    {
                        if (tarih != string.Empty)
                        {
                            string ay = tarih.Substring(tarih.IndexOf('-') + 1, ((tarih.LastIndexOf('-')) - (tarih.IndexOf('-'))) - 1);
                            switch (ay)
                            {
                                case "Jan": ayim = "Ocak";
                                    break;
                                case "Feb": ayim = "Subat";
                                    break;
                                case "Mar": ayim = "Mart";
                                    break;
                                case "Apr": ayim = "Nisan";
                                    break;
                                case "May": ayim = "Mayis";
                                    break;
                                case "Jun": ayim = "Haziran";
                                    break;
                                case "Jul": ayim = "Temmuz";
                                    break;
                                case "Aug": ayim = "Agustos";
                                    break;
                                case "Sep": ayim = "Eylül";
                                    break;
                                case "Oct": ayim = "Ekim";
                                    break;
                                case "Nov": ayim = "Kasim";
                                    break;
                                default: ayim = "Aralik";
                                    break;
                            }
                            //Tarih'i düzenle
                            char[] ch = tarih.ToCharArray();
                            for (int i = tarih.LastIndexOf('-') + 1; i < tarih.Length - 1; i++)
                            {
                                tarihim[counttarihim] += tarih[i].ToString();
                            }
                            tarihim[counttarihim] += " " + ayim + " " + tarih.Substring(0, tarih.IndexOf('-'));
                            counttarihim++;
                        }
                    }
                    tbWacilisTarihi.Text = tarihim[0];
                    tbWbitisTarihi.Text = tarihim[1];
                    tbWguncellemeTarihi.Text = tarihim[2];
                }
                catch
                {
                    MessageBox.Show("Sunucuya Baglanilamiyor veya Böyle Bir Web Sayfasi Yok", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            #region TR Degil
            else
            {
 
                try
                {
                    client.Connect("whois.internic.com", 43);
                    domain = textBox1.Text + System.Environment.NewLine;
                    diziDomain = Encoding.ASCII.GetBytes(domain.ToCharArray());
                    str = client.GetStream();
                    str.Write(diziDomain, 0, domain.Length);
                    oku = new StreamReader(client.GetStream(), Encoding.ASCII);
                    int countns = 0, countdate = 0;
 
                    while ((satir = oku.ReadLine()) != null)
                    {
                        if ((satir.Trim().StartsWith("NS")) || satir.Trim().StartsWith("Name"))
                        {
                            ns[countns] = satir.Substring(satir.IndexOf(':') + 1).Trim();
                            countns++;
                        }
                        if (satir.ToUpper().Trim().IndexOf(" date".ToUpper()) >= 0)
                        {
                            date[countdate] = satir.Substring(satir.IndexOf(':') + 1).Trim();
                            countdate++;
                        }
                        if (countns == 2 && countdate == 3)
                            break;
                    }
                    tbWns1.Text = ns[0];
                    tbWns2.Text = ns[1];
                    string ayim;
                    int counttarihim = 0;
                    foreach (string tarih in date)
                    {
                        string ay = tarih.Substring(tarih.IndexOf('-') + 1, ((tarih.LastIndexOf('-')) - (tarih.IndexOf('-'))) - 1);
                        switch (ay)
                        {
                            case "jan": ayim = "Ocak";
                               break;
                            case "feb": ayim = "Subat";
                                break;
                            case "mar": ayim = "Mart";
                                break;
                            case "apr": ayim = "Nisan";
                                break;
                            case "may": ayim = "Mayis";
                                break;
                            case "jun": ayim = "Haziran";
                                break;
                            case "jul": ayim = "Temmuz";
                                break;
                            case "aug": ayim = "Agustos";
                                break;
                            case "sep": ayim = "Eylül";
                                break;
                            case "oct": ayim = "Ekim";
                                break;
                            case "nov": ayim = "Kasim";
                                break;
                            default: ayim = "Aralik";
                                break;
                        }
                        char[] ch = tarih.ToCharArray();
                        for (int i = 0; i < tarih.IndexOf('-'); i++)
                        {
                            tarihim[counttarihim] += tarih[i].ToString();
                        }
                        tarihim[counttarihim] += " " + ayim + " " + tarih.Substring(tarih.LastIndexOf('-') + 1);
                        counttarihim++;
                    }
                }
                catch
                {
                    MessageBox.Show("Sunucuya Baglanilamiyor veya Böyle Bir Web Sayfasi Yok", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
 
                tbWguncellemeTarihi.Text = tarihim[0];
                tbWacilisTarihi.Text = tarihim[1];
                tbWbitisTarihi.Text = tarihim[2];
            }
            #endregion
        }

 

Veysel Ugur KIZMAZ