<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GetHostAndIP.aspx.cs" Inherits="GetHostAndIP" %>
<html xmlns="[url]http://www.w3.org/1999/xhtml[/url]" >
<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">
<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>
</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>
<html xmlns="[url]http://www.w3.org/1999/xhtml[/url]" >
<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">
<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>
</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;
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 + " Value:" + headers.Get(strCookies));
}
for (int i = 0; i < headers.Count; i++)
{
strCookies = headers.GetKey(i);
Response.Write("<br>Name:" + strCookies + " Value:" + headers.Get(strCookies));
}
//得到主机名和IP
string hostName = Dns.GetHostName();
IPAddress[] ip = Dns.GetHostAddresses(hostName);
Response.Write("ServerName : " + hostName + " IP:  " + 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;
}
}
string hostName = Dns.GetHostName();
IPAddress[] ip = Dns.GetHostAddresses(hostName);
Response.Write("ServerName : " + hostName + " IP:  " + 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 地址和别名。
IPHostEntry:为 Internet 主机地址信息提供容器类。
程序中用到的属性:
AddressList 获取或设置与主机关联的 IP 地址列表。
HostName 获取或设置主机的 DNS 名称。
Dns静态类:提供简单的域名解析功能,它从 Internet 域名系统 (DNS) 检索关于特定主机的信息。在 IPHostEntry 类的实例中返回来自 DNS 查询的主机信息。如果指定的主机在 DNS 数据库中有多个入口,则 IPHostEntry 包含多个 IP 地址和别名。
本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/6970,如需转载请自行联系原作者