新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:Whois search using C#(感觉不错。以前C++和VB的都做过。现在换一个C#的看看)

Whois search using C#(感觉不错。以前C++和VB的都做过。现在换一个C#的看看)

新客网 XKER.COM 2003-07-12 来源: 收藏本文

Introduction

Quite often you want to know who owns a given domain. To obtain the registry information, you go to the respective registry and start a so called WHOIS query (lookup). The trick is that you have to know which registry is responsible for which TLD (Top Level Domain).
The database is the so called WHOIS database and it has one distinct property: it provides us with a query interface via TCP port 43! And as the .NET framework provides us with the TCPClient class, we can use this interface to directly obtain our data The following example is a minimal implementation of a WHOIS lookup (whois.aspx):
<% @Page Language="C#" %><% @Assembly Name="System.Net" %><% @Import Namespace="System.Net.Sockets" %><% @Import Namespace="System.Text" %><% @Import Namespace="System.IO" %><%TCPClient tcpc = new TCPClient();if (0 == tcpc.Connect("whois.networksolutions.com", 43)){     String strDomain = "microsoft.com\r\n";    Byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain.ToCharArray());    Stream s = tcpc.GetStream();    s.Write(arrDomain, 0, strDomain.Length);    StreamReader sr = new StreamReader(tcpc.GetStream(), Encoding.ASCII);    while (-1 != sr.Peek())    {        Response.Write(sr.ReadLine());    }    tcpc.Close();}else{    Response.Write("Could not connect to WHOIS server!");}%>

To be able to work with the TCPClient class, we need the System.Net.Sockets namespace. We also need to add classes like System.Text or System.IO. Most of the work in this example is being done by the TCPClient class: using it, I can connect to a server on any port (the Connect method does that) and send data to and fro (via Stream). Sending the query warrants a little explanation: for sending I use the binary stream without any wrappers. Therefore, I have to turn the query string (CR/LF terminated) into a byte array (with ASCII encoding) and can then send the desired number of bytes to the server.  
Fetching the data happens via the StreamReader, which allows comfortable and direct work with the character data. And to keep the example simple to the end, the received data are immediately passed through to the client. 
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐