Question Trying to download whois data for multiple websites but I get an error please help

Issue

New member
Joined
May 17, 2012
Messages
1
Programming Experience
5-10
The aim of my program is to download whois data to a textfile from multiple websites whoes addresses are within the file "WebsiteList.txt" but I keep receiveing the error -"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."

Below is my program so far and attached is first the list of websites named WebsiteList.txt and the server file which needs to be in the bin directory. (For you to use this code/program you need to rename it from servers.lst.txt to servers.txt)


Could someone please get this to work for me. Thank you, I just can't seem to solve this problem.

Imports System.IO
Imports System.Text
Imports System.Net.Sockets
Public Class Form1
    Dim FILEtoWrite As String = "c:\users\Andrew\Desktop\WhoisData.txt"
    Dim FILEtoRead As String = "c:\users\Andrew\Desktop\WebsiteList.txt"
    Private whoisTable As New Hashtable()
    Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click

        If System.IO.File.Exists(FILEtoRead) = True Then

            Dim objReader As New System.IO.StreamReader(FILEtoRead)

            Do While objReader.Peek() <> -1
                ListBox1.Items.Add(objReader.ReadLine())
            Loop

        End If

    End Sub

    Sub Lookup(ByVal domain As String)
        Dim i As Int32
        Dim lines As String
        Dim server As String
        Dim tld As String


        If (isDomain(domain.Trim)) Then
            tld = domain.Substring(domain.IndexOf(".") + 1)
            server = whoisTable.Item(tld)
        Else
            Exit Sub
        End If
        ' do the whois query
        Dim whoisResult As String = doWhoisQuery(domain, server)
        ' send the output to the results textbox
        lines += "[" + server + "]" + vbLf
        lines += whoisResult + vbLf
        ' check for a redirect whois server
        Dim infoServer As String = GetInfoServer(whoisResult)
        If (infoServer <> "") Then
            whoisResult = doWhoisQuery(domain, infoServer)
            lines += "[" + infoServer + "]" + vbLf
            lines += whoisResult
        End If
        DownloadtoFile(lines)
    End Sub
    Sub DownloadtoFile(ByVal Lines As String)
        Dim objWriter As New System.IO.StreamWriter(FILEtoWrite, True)
        objWriter.WriteLine(Lines)
        objWriter.WriteLine()
        objWriter.Close()
    End Sub
    Private Function doWhoisQuery(ByVal domain As String, ByVal server As String) As String
        Dim returnData As String = ""
        Dim tcpClient As New TcpClient()
        tcpClient.Connect(server, 43)
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        If networkStream.CanWrite And networkStream.CanRead Then
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(domain + vbCrLf)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            Dim recvSize As Int32
            recvSize = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            While (recvSize <> 0)
                returnData += Encoding.ASCII.GetString(bytes, 0, recvSize)
                recvSize = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            End While
        Else
            If Not networkStream.CanWrite Then
                returnData = "Cannot write data to this stream."
            Else
                If Not networkStream.CanRead Then
                    returnData = "Cannot read data from this stream."
                End If
            End If
        End If
        tcpClient.Close()
        Return returnData
    End Function

    Private Function isDomain(ByVal domain As String) As Boolean
        Dim regex As New RegularExpressions.Regex("^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$")
        Return regex.IsMatch(domain)
    End Function

    Private Shared Function LoadWhoisServers(ByVal filename As String) As Hashtable
        Dim tmpArray() As String
        Dim whoisTable As New Hashtable()
        Try
            Dim MyStreamReader As StreamReader = IO.File.OpenText(filename)
            Do While MyStreamReader.Peek() >= 0
                tmpArray = MyStreamReader.ReadLine().Split("|")
                If (tmpArray.GetLength(0) = 6 And tmpArray(0).StartsWith("#") = False) Then
                    whoisTable.Add(tmpArray(0), tmpArray(1))
                End If
            Loop
            MyStreamReader.Close()
        Catch e As Exception
            MsgBox(e.Message + vbLf + "The application will now exit.", MsgBoxStyle.Critical)
            End
        End Try
        Return whoisTable
    End Function

    Private Shared Function GetInfoServer(ByVal queryTxt As String) As String
        Dim regex As New RegularExpressions.Regex("Whois Server:\s+(.+)", RegularExpressions.RegexOptions.IgnoreCase)
        Dim match As RegularExpressions.Match = regex.Match(queryTxt)
        If (match.Value <> "") Then
            Return match.Value.Substring(14)
        Else
            Return ""
        End If
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        whoisTable = LoadWhoisServers("servers.lst")
    End Sub

    Private Sub DownloadWhoisInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadWhoisInfo.Click
        For i = 0 To ListBox1.Items.Count - 1
            Lookup(ListBox1.Items(i))
        Next
    End Sub
End Class

 

Attachments

  • WebsiteList.txt
    1 KB · Views: 16
  • servers.lst.txt
    19.5 KB · Views: 13
Last edited by a moderator:
Back
Top