名称:域名信息查询工具 C#版
运行环境:IIS + .net Framework 2.0
使用方法:
1、查询域名信息
Domain.aspx?domain=digitwest.com
2、查询域名状态
Domain.aspx?op=status&domain=digitwest.com
演示地址:http://www.digitwest.com/Plus/Domain.aspx?domain=digitwest.com
时间: 2009-8-28
作者: 海涛
版本: 1.0
描述:
用C#实现的域名信息查询工具,直接查询WHOIS服务器的方法实现
博客地址:http://haitao.name QQ:12496785 Email:kingter520@163.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | /** * 时间: 2009-8-28 * 作者: 海涛 * 版本: 1.0 * 描述: * 域名信息查询 * * 博客地址:http://haitao.name QQ:12496785 Email:kingter520@163.com * * 使用方法: * 1、查询域名信息 * Domain.aspx?domain=digitwest.com * 2、查询域名状态 * Domain.aspx?op=status&domain=digitwest.com */ using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net.Sockets; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.IO; public partial class Domain : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "text/plain"; string domain = Request.QueryString["domain"]; string domainOp = Request.QueryString["op"]; if (string.IsNullOrEmpty(domain)) return; if (domainOp == "status") Response.Write(GetDomainStatus(domain, "com")); else Response.Write(GetDomainContract(domain)); } /// <summary> /// 域名注册信息 /// </summary> /// <param name="domain">输入域名,不包含www</param> /// <returns></returns> public static string GetDomain(string domain) { string strServer; string whoisServer = "whois.internic.net,whois.cnnic.net.cn,whois.publicinterestregistry.net,whois.nic.gov,whois.hkdnr.net.hk,whois.nic.name"; string[] whoisServerList = Regex.Split(whoisServer, ",", RegexOptions.IgnoreCase); if (domain == null) throw new ArgumentNullException(); int ccStart = domain.LastIndexOf("."); if (ccStart < 0 || ccStart == domain.Length) throw new ArgumentException(); //根据域名后缀选择服务器 string domainEnd = domain.Substring(ccStart + 1).ToLower(); switch (domainEnd) { default: //.COM, .NET, .EDU strServer = whoisServerList[0]; break; case "cn": //所有.cn的域名 strServer = whoisServerList[1]; break; case "org": //所有.org的域名 strServer = whoisServerList[2]; break; case "gov": //所有.gov的域名 strServer = whoisServerList[3]; break; case "hk": //所有.hk的域名 strServer = whoisServerList[4]; break; case "name": //所有.name的域名 strServer = whoisServerList[5]; break; } string ret = ""; Socket s = null; try { string cc = domain.Substring(ccStart + 1); s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.SendTimeout = 900; s.Connect(new IPEndPoint(Dns.Resolve(strServer).AddressList[0], 43)); s.Send(Encoding.ASCII.GetBytes(domain + "\\r\\n")); byte[] buffer = new byte[1024]; int recv = s.Receive(buffer); while (recv > 0) { ret += Encoding.UTF8.GetString(buffer, 0, recv); recv = s.Receive(buffer); } s.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { return ex.Message; } finally { if (s != null) s.Close(); } return ret; } /// <summary> /// 指定whois查询域名信息 /// </summary> /// <param name="domain">输入域名,不包含www</param> /// <param name="strServer">输入whois</param> /// <returns></returns> public static string GetDomain(string domain, string strServer) { if (domain == null) throw new ArgumentNullException(); int ccStart = domain.LastIndexOf("."); if (ccStart < 0 || ccStart == domain.Length) throw new ArgumentException(); string ret = ""; Socket s = null; try { string cc = domain.Substring(ccStart + 1); s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.SendTimeout = 900; s.Connect(new IPEndPoint(Dns.Resolve(strServer).AddressList[0], 43)); s.Send(Encoding.ASCII.GetBytes(domain + "\\r\\n")); byte[] buffer = new byte[1024]; int recv = s.Receive(buffer); while (recv > 0) { ret += Encoding.UTF8.GetString(buffer, 0, recv); recv = s.Receive(buffer); } s.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { return ex.Message; } finally { if (s != null) s.Close(); } return ret; } /// <summary> /// 域名注册信息和联系人信息 /// </summary> /// <param name="domain">输入域名,不包含www</param> /// <returns></returns> public string GetDomainContract(string domain) { string whoisInfo = ""; whoisInfo = GetDomain(domain); //取域名联系人 string str = "Whois Server:"; StringReader sr = new StringReader(whoisInfo); string newline = ""; while (sr.Peek() > 0) { newline = sr.ReadLine(); newline = newline.Trim(); if (newline.StartsWith(str)) break; } sr.Close(); int retPos = newline.IndexOf(str); if (retPos != -1) { string newWhois = newline.Substring(str.Length).Trim(); whoisInfo += "\\r\\n" + GetDomain(domain, newWhois); } return whoisInfo; } /// <summary> /// 域名状态 /// </summary> /// <param name="domain">输入域名,不包含www</param> /// <param name="domainType">域名类型(国际域名:com|国内域名:cn)</param> /// <returns></returns> public string GetDomainStatus(string domain, string domainType) { string whoisInfo = ""; string Status = ""; whoisInfo = GetDomain(domain); //取域名联系人 string str = "Status:"; if (domainType == "cn") str = "Domain Status:"; StringReader sr = new StringReader(whoisInfo); string newline = ""; while (sr.Peek() > 0) { newline = sr.ReadLine(); newline = newline.Trim(); if (newline.StartsWith(str)) break; } sr.Close(); int retPos = newline.IndexOf(str); if (retPos != -1) Status = newline.Substring(str.Length).Trim(); else Status = whoisInfo; return Status; } } |
下载:域名信息查询工具 C#版



