quarta-feira, 7 de agosto de 2013

Consultar DNS - IP com C#

Este tutorial irá mostrar como executar uma consulta DNS (PING) usando o System.Net NET, ASP.NET 2.0 e C #.



Veja o código:

Default.aspx



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head runat="server">
    <title>DNS Consulta - Edgar Esteves</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>

<tr>

<td align="right" bgcolor="#eeeeee" class="header1">Nome do Dominio::</td>
<td bgcolor="#FFFFFF"><asp:textbox ID="txtWWW" runat="server" Enabled="False"
        Width="48px">www.</asp:textbox><asp:textbox ID="txtDominio" runat="server"></asp:textbox><br />
<asp:button ID="btnProcurar" runat="server" OnClick="btnProcurar_Click"
        Text="Procurar" /></td>
</tr>
<tr>

<td width="100" align="right" bgcolor="#eeeeee" class="header1">Endereço IP do
    Dominio:</td>
<td bgcolor="#FFFFFF"> &nbsp;<br />
<asp:textbox ID="txtIPs" runat="server"></asp:textbox><br />
<br />
<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>
    </div>
    </form>
</body>
</html>


Default.aspx.CS

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnProcurar_Click(object sender, EventArgs e)
    {

        DNSProcurar(txtDominio.Text);
    }
    protected void DNSProcurar(string dominio)
    {

        try
        {

            //performs the DNS lookup
            IPHostEntry he = Dns.GetHostByName(dominio);
            IPAddress[] ip_addrs = he.AddressList;
            txtIPs.Text = "";
            foreach (IPAddress ip in ip_addrs)
            {

                txtIPs.Text += ip + " ";
            }
        }
        catch (System.Exception ex)
        {

            lblStatus.Text = ex.ToString();
        }
    }

}

0 comentários:

Postar um comentário