ASP.Net WhoIs LookUp - Page 2

Posted: 13/02/2005 14:32:08
Author: Andy Pilgrim
Is ASP.Net 1.x Compatible? Yes! ASP.Net 1.x Compatible (?)

    Recap

    On Page 1 we discussed the basic nuts and bolts of the whois lookup tool, however the results returned by the code do not really look that good, nor are they particularly informative. Here we look to improve the way we format the results and the type of results we return.

    Formatting Results

    The whois server supplies the results in clear text format. When we put it onto a webpage, because HTML ignores line breaks - we lose the formatting. To overcome this we need to convert line feeds into the (X)HTML element <br/>. This is best done using a simple regular expression.

    RegEx.Replace(WhoIsResult,"\n","</br>")

    The code searches for instances of \n, a new line and replaces them with <br/> a HTML line break. By formatting the return values of the WhoIsLookup function with this regular expression the result viewed in the browser is much easier to read.

    Returning Better Results

    Unless by chance you query the whois is server of the registrar of the domain you are looking up, you wont get the full whois record. To get information about the owner of the domain you need to query the correct whois server. Luckily when you query a whois server which does not contain the full record, it returns, amongst lots of other stuff, the whois server where you can find the full record. Here regular expressions come to the rescue again. Before we look at the code, let's just consider what happens if the domain is not regiestered.

    Checking for Matches

    If the domain you searched for is not registered the whois server will return 'No match for domain "MYDOMAIN.COM"'. We can use the Regex class to search for this eventuality, and proceed accordingly. The final code, with added verbose output is:

    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Net" %>
    <html>
    <head>
    <title>ASP.Net WhoIs LookUp Article</title>
    <style>
    body {font-family:verdana}
    </style>
    </head>
    <body>
     <form runat="server">
      <h1>WhoIs LookUp Tool</h1>
      Domain: <asp:textbox runat="server" id="Domain" />
      <asp:button onclick="doLookUp" runat="server" text="LookUp"/>
      <h2>Results</h2>
      <p><asp:label runat="server" id="output" forecolor="red"/></p>
      <p><asp:label runat="server" id="whoisrecord" /></p>
     </form>
    </body>
    </html>
    
    <script runat="server">
    Sub DoLookUp(Sender As Object, e As EventArgs)
      'Use the primary whois server to get whois info
      Dim WhoIsResult As String
      WhoIsResult = WhoIsLookUp("domain " & Domain.Text, _ 
          "rs.internic.net")
      Output.Text = "Looking Up " & Domain.Text & _
          " using rs.internic.net...<br/>"
      'Check whether a result was returned
      If Regex.Match(WhoIsresult, "No match for domain """ & _
          UCase(Domain.Text) & """").Success Then
        Output.Text &= "No match found!"
      Else
        'Extract the whois server hosting the full record
        Dim SecondaryServer As String
        SecondaryServer = Regex.Match(WhoIsresult, _
            "Whois Server: (\S+)").Groups(1).ToString()
        Output.Text &= "Going to " & SecondaryServer & _
            " for full record...<br/>"
        'Now lookup using secondary server
        WhoIsResult = WhoIsLookUp(Domain.Text,SecondaryServer)	
        Output.Text &= "Record successfully retrieved."
      End If
      WhoIsResult = RegEx.Replace(WhoIsResult,"\n","</br>")
      WhoIsRecord.Text = WhoIsResult
    End Sub
    ...
    </script>

    So what does the code do? First of all we use a default whois server to perform a lookup, here we use rs.internic.net. Then we use the Regex class to check whether the server has returned a non-match. If it has we inform the user and return the evidence. Otherwise we use a regular expression to find the correct whois server to ask for the full record. Grabbing this, we perform another call to the WhoIsLookUp function and return the full record to screen.

    Demo and Source Download

    A live demo of the code in action can be found at:
    http://demo.semichaos.com/whois.aspx

    You can also download the code from:
    http://download.semichaos.com/whois.zip