C# whois domain--ASP.NET, WindowsForm

FROM: http://www.geekpedia.com/tutorial231_Getting-the-Who-Is-Information-of-a-Hostname.html

http://www.aspdev.org/articles/build-whois-lookup-asp.net/

 

Windows Forms代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 using System.IO;
 9 using System.Net.Sockets;
10 
11 namespace WhoIs
12 {
13     public partial class Form1 : Form
14     {
15         // Networking and IO objects we'll be using throughout the application
16         TcpClient tcpWhois;
17         NetworkStream nsWhois;
18         BufferedStream bfWhois;
19         StreamWriter swSend;
20         StreamReader srReceive;
21 
22         public Form1()
23         {
24             InitializeComponent();
25         }
26 
27         private void Form1_Load(object sender, EventArgs e)
28         {
29             // Has the first value selected
30             cmbServer.SelectedIndex = 0;
31         }
32 
33         private void btnLookup_Click(object sender, EventArgs e)
34         {
35             try 
36             {
37                 // The TcpClient should connect to the who-is server, on port 43 (default who-is)
38                 tcpWhois = new TcpClient(cmbServer.SelectedItem.ToString(), 43);
39                 // Set up the network stream
40                 nsWhois = tcpWhois.GetStream();
41                 // Hook up the buffered stream to the network stream
42                 bfWhois = new BufferedStream(nsWhois);
43             }
44             catch
45             {
46                 MessageBox.Show("Could not open a connection to the Who-Is server.");
47             }
48             // Send to the server the host-name that we want to get information on
49             swSend = new StreamWriter(bfWhois);
50             swSend.WriteLine(txtHostName.Text);
51             swSend.Flush();
52             // Clear the textbox of anything existing content
53             txtResponse.Clear();
54             try 
55             {
56                 srReceive = new StreamReader(bfWhois);
57                 string strResponse;
58                 // Read the response line by line and put it in the textbox
59                 while ((strResponse = srReceive.ReadLine()) != null
60                 {
61                     txtResponse.Text += strResponse + "\r\n";
62                 }
63             }
64             catch
65             {
66                 MessageBox.Show("Could not read data from the Who-Is server.");
67             }
68             // We're done with the connection
69             tcpWhois.Close();
70         }
71     }
72 }

 

 

 

Web Form代码
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebStat.WebForm3" %>
 2 <% @Import Namespace="System.Net.Sockets" %>
 3 <% @Import Namespace="System.Text" %>
 4 <% @Import Namespace="System.IO" %>
 5 <% @Import Namespace="System.Text.RegularExpressions" %> 
 6 <script language="C#" runat="server">
 7 
 8 public void btn_Click(object sender, EventArgs eArgs)
 9 {
10 try
11 {
12 TcpClient objTCPC = new TcpClient(Request.Form["WhoisServer"], 43);
13 string strDomain = Request.Form["DomainName"+ "\r\n";
14 byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain);
15 
16 Stream objStream = objTCPC.GetStream();
17 objStream.Write(arrDomain, 0, strDomain.Length);
18 StreamReader objSR = new StreamReader(objTCPC.GetStream(),
19 Encoding.ASCII);
20 lblResponse.Text = "<b>" + Request.Form["DomainName"+
21 "</b><br><br>" + Regex.Replace(objSR.ReadToEnd(),"\n","<br>");
22 
23 objTCPC.Close();
24 }
25 catch(Exception ex)
26 {
27 lblResponse.Text = ex.ToString();
28 }
29 }
30 
31 </script>
32 
33 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
34 
35 <html xmlns="http://www.w3.org/1999/xhtml" >
36 <head runat="server">
37     <title>无标题页</title>
38     <style type="text/css">
39 .main {font-family:Verdana; font-size:12px;}
40 .title {font-family:Verdana; font-size:18px; font-weight:bold;}
41 </style> 
42 </head>
43 <body>
44 <form id="Form2" method="POST" name="MainForm" runat="server">
45 
46     <div>
47     <span class="title" align="center">WHOIS ASP.NET page</span>
48 
49 
50 <table>
51 <tr>
52 <td class="main" align="right">Whois Server</td>
53 <td class="main">
54 <asp:DropDownList class="main" id="WhoisServer" runat="server">
55 <asp:ListItem value="whois.networksolutions.com">
56 whois.networksolutions.com (.COM, .NET, .EDU)</asp:ListItem>
57 <asp:ListItem value="whois.ripe.net">whois.ripe.net
58 (Europe)</asp:ListItem>
59 <asp:ListItem value="whois.cira.ca">whois.cira.ca (.CA)
60 </asp:ListItem>
61 <asp:ListItem value="whois.nic.uk">whois.nic.uk
62 (.CO.UK)</asp:ListItem>
63 <asp:ListItem value="whois.domain-registry.nl">
64 whois.domain-registry.nl (.NL)</asp:ListItem>
65 </asp:DropDownList>
66 </td>
67 </tr>
68 <tr>
69 <td class="main" align="right">Domain Name:</td>
70 <td class="main"><input type="text" class="main" name="DomainName" value=""/></td>
71 </tr>
72 <tr>
73 <td class="main"> </td>
74 <td class="main"><input type="Submit" id="btnSubmit" onserverclick="btn_Click" value="Send" runat="server" /></td>
75 </tr>
76 </table>
77 <br/><br/>
78 <asp:Label class="main" id="lblResponse" runat="server"/> 
79     </div>
80     </form>
81 </body>
82 </html>
83 

 

 

posted @ 2010-12-15 18:58  ®Geovin Du Dream Park™  阅读(535)  评论(0编辑  收藏  举报