博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET 例程完全代码版(4)——DNS静态类
阅读量:7209 次
发布时间:2019-06-29

本文共 2823 字,大约阅读时间需要 9 分钟。

GetHostAndIP.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetHostAndIP.aspx.cs" Inherits="GetHostAndIP" %>
<html xmlns=" " >
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 925px; height: 180px">
            <tr>
                <td style="width: 44px">
                    <asp:TextBox ID="txtDomain" runat="server"></asp:TextBox></td>
                <td style="width: 3px">
                    <asp:Button ID="btnOK" runat="server" Text="转换为IP地址:" OnClick="btnOK_Click" /></td>
                <td style="width: 116px">
                    &nbsp;<asp:Label ID="lblMsg" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 44px; height: 76px;">
                    <asp:TextBox ID="txtIP" runat="server"></asp:TextBox>&nbsp;
                </td>
                <td style="width: 3px; height: 76px;">
                    <asp:Button ID="Button1" runat="server"   Text="对应域名为:" OnClick="Button1_Click"/></td>
                <td style="width: 116px; height: 76px;">
                    <asp:Label ID="lblDomain" runat="server" Text=""></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 44px; height: 30px;">
                </td>
                <td style="width: 3px; height: 30px;">
                </td>
                <td style="width: 116px; height: 30px;">
                </td>
            </tr>
        </table>    
    </div>
    </form>
</body>
</html>
code behind代码:GetHostAndIP.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Collections.Specialized;
using System.Net;
public partial class GetHostAndIP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //得到当前浏览器头信息
        NameValueCollection headers = new NameValueCollection();
        headers = Request.Headers;
        string strCookies = null;
        for (int i = 0; i < headers.Count; i++)
        {
            strCookies = headers.GetKey(i);
            Response.Write("<br>Name:" + strCookies + "&nbsp;&nbsp;&nbsp;&nbsp;Value:" + headers.Get(strCookies));
        }
        //得到主机名和IP
        string hostName = Dns.GetHostName();
        IPAddress[] ip = Dns.GetHostAddresses(hostName);
        Response.Write("ServerName :&nbsp" + hostName + "&nbsp;&nbsp;&nbsp;IP:&nbsp&nbsp" + ip[0].ToString());
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        IPAddress[] ip = Dns.GetHostAddresses(txtDomain.Text);
        lblMsg.Text = ip[0].ToString();
        //or
        //IPHostEntry hostInfo = Dns.GetHostByName(txtDomain.Text);//gets the DND info for the specified DNS host name
        //lblMsg.Text = hostInfo.AddressList[0].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        IPHostEntry hostInfo = Dns.GetHostEntry(txtIP.Text);
        lblDomain.Text = hostInfo.HostName;
    }
}
说明:
IPHostEntry:为 Internet 主机地址信息提供容器类。
程序中用到的属性:
   AddressList  获取或设置与主机关联的 IP 地址列表。 
   HostName     获取或设置主机的 DNS 名称。 
Dns静态类:提供简单的域名解析功能,它从 Internet 域名系统 (DNS) 检索关于特定主机的信息。在 IPHostEntry 类的实例中返回来自 DNS 查询的主机信息。如果指定的主机在 DNS 数据库中有多个入口,则 IPHostEntry 包含多个 IP 地址和别名。
本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/6970,如需转载请自行联系原作者
你可能感兴趣的文章
微信小程序基本入门
查看>>
oracl 数字型函数
查看>>
Q443 压缩字符串
查看>>
Bootstrap——网站添加字体图标
查看>>
MVC传递数据-传递对象或对象集合
查看>>
单页应用的三大优势及监控方法
查看>>
菜鸟调错(三)——Jboss与jdk版本号不兼容导致WebService调用出错
查看>>
你是那种仅仅看《XXXXX从入门到精通》的程序猿吗?
查看>>
Python开发【第一篇】:目录
查看>>
《新闻发布》解析
查看>>
C++知识点
查看>>
FTP传输文件被破坏的问题(Linux、Busybox)
查看>>
242. Valid Anagram
查看>>
P1024 一元三次方程求解(二分答案)
查看>>
Collections库使用
查看>>
SQL Server开启READ_COMMITTED_SNAPSHOT
查看>>
Linux学习7之Shell基础--Bash的变量
查看>>
VirtualBox虚拟机增加CentOS根目录容量 LVM扩容
查看>>
Nginx 和 PHP 的两种部署方式比较
查看>>
纪录2b,和诡异,
查看>>