Search code examples
vb.netstreamreaderwebrequestwebresponse

Issue with StreamReader


I am writing code where I am trying to grab the HTML from a DNS report online (http://viewdns.info/dnsreport/?domain=google.com), but I am having some issues. The one line of the HTML file (Line 231) that I actually need is cutting itself off after around 680 characters. All of the lines after the important one are reading correctly, however. The code for grabbing the HTML is shown below, and I have tried it in two separate ways. This is the first way I tried:

Public Function getWebResourceData(ByVal strURL As String) As String
    Dim webClient As New System.Net.WebClient
    Dim result As String = webClient.DownloadString("http://viewdns.info/dnsreport/?" &        TextBox1.Text)
    return result
End Function

And this is the second:

Public Function getWebResourceData(ByVal strURL As String) As String
    Dim rt As String = ""
    Dim wRequest As WebRequest
    Dim wResponse As WebResponse
    Dim SR As StreamReader
    wRequest = WebRequest.Create(strURL)
    wResponse = wRequest.GetResponse
    SR = New StreamReader(wResponse.GetResponseStream)
    rt = SR.ReadToEnd
    SR.Close()
    return rt
End Function

Im really not sure what else could be wrong at this point. I have also tried saving the result to a text file to see if that was the issue, but that was incorrect as well. I have looked into the hex codes for the area where the string is stopping, but there isn't anything out of the ordinary. The split occurs between the back to back alligator brackets (shown as parentheses) here: (/tr)(tr)

But there are numerous sets of these tags throughout the HTML that there are no issues with.


Solution

  • Both of your functions don't return what they have read. I have tested the second one and it works correctly.

    Sub Main
        Dim ret = getWebResourceData("http://viewdns.info/dnsreport/?domain=google.com")
        Console.WriteLine(ret.Length)
         ' Output = 21605
    End Sub
    
    Public Function getWebResourceData(ByVal strURL As String) As String
        Dim rt As String = ""
        Dim wRequest As WebRequest
        Dim wResponse As WebResponse
        Dim SR As StreamReader
        wRequest = WebRequest.Create(strURL)
        wResponse = wRequest.GetResponse
        SR = New StreamReader(wResponse.GetResponseStream)
        rt = SR.ReadToEnd
        SR.Close()
        return rt
    End Function