I have the following code for reading an online file, somehow the first line returns an empty string, what can be the reason for this?

Dim myRequest As WebRequest = webRequest.Create(myurl)
myRequest.Proxy = WebRequest.DefaultWebProxy
Dim mystream As Stream = myRequest.GetResponse.GetResponseStream()
Dim objReader As New StreamReader(mystream)

Dim sLine As String = ""
Dim i As Integer = 0
ListAnrufe.Items.Clear() 'this is a listbox
Do While Not sLine Is Nothing
i += 1
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
ListAnrufe.Items.Add(sLine)

End If
Loop

Recommended Answers

All 9 Replies

I tried your code with Dim myRequest As WebRequest = WebRequest.Create("http://www.daniweb.com") and got no empty line as the first line.

Have you tried other URLs? What if you comment out myRequest.Proxy = WebRequest.DefaultWebProxy line, does it help?

I can't see any obvious reason why you should get an empty line, unless the web page has an "empty" line as the first line.

One comment about your code. Remember to close the reader and streams and dispose them to release the resources.

HTH

I removed the line .proxy, but it didn't make a difference. And no, the first line is not empty, it has data, which just get skipped over. I am so confused. Maybe I should add, that this is a textfile, it doesn't have any html tags.

Ok. I tried in my localhost Dim myRequest As WebRequest = WebRequest.Create("http://127.0.0.1/test.txt") and got no empty lines from text file with four lines of plain text.

I also asked, have you tried other URLs? Like Dim myRequest As WebRequest = WebRequest.Create("http://www.daniweb.com") which didn't produce any empty lines for me.

You could also open the URL you're using with browser and use "View Source" to check if the text file contains an empty line (or simple CR/LF pair) at the beginning.

One quick&dirty solution would be to check if the first line is empty. If it is, skip it. You already have a counter in your code

i += 1
sLine = objReader.ReadLine
If i = 1 AndAlso String.IsNullOrEmpty(sLine) Then
  Continue Do
End If

Not an elegant solution but it should work ;)

As I said, the first line is not empty, instead of giving me the first line, it gives me an empty string and the next line it gives me, is the second line. I do not get the first line. In view source, I see a space before the line, but even if that is a crlf, it should give me the rest of the line as the second line, but it doesn't. I'll try a different website, and let you know what happens

Another thing, that seemed strange, was that I couldn't use .readtoend, somehow it only gave me an empty string.

I tested objReader.ReadToEnd with http://127.0.0.1/test.txt and it worked just fine. Of course you have to split the input to get separate lines. Anyway, objReader.ReadToEnd returned the whole content of the file as supposed to.

Have you stepped through the code? I mean, put a break point (F9) at line sLine = objReader.ReadLine and step the code (F8). Is sLine really an empty (or zero length string) when that line is executed at first time?

I use Win XP Pro, .NET 2.0, VB.2005 and I do assume we are using similar (enough) systems. It's very confusing why I can't reproduce that anomaly you have :confused:

Damn :) I was able to reproduce it. The file contains (or starts) with a zero byte. That's why the first line looks like an empty string (but it isn't).

Try this

Dim myRequest As WebRequest = WebRequest.Create("http://127.0.0.1/test.txt")
myRequest.Proxy = WebRequest.DefaultWebProxy
Dim mystream As Stream = myRequest.GetResponse.GetResponseStream()
Dim objReader As New StreamReader(mystream)

Dim sLine As String = ""
Dim i As Integer = 0
ListBox1.Items.Clear() 'this is a listbox
Do While Not sLine Is Nothing
  i += 1
  sLine = objReader.ReadLine
  If Not sLine Is Nothing Then
    sLine = sLine.Replace(System.Convert.ToChar(0), "")
    ListBox1.Items.Add(sLine)
  End If
Loop

The line sLine = sLine.Replace(System.Convert.ToChar(0), "") removes null bytes. I didn't test if some other white space (i.e. non-printable) character could cause that anomaly.

commented: Stays with the problem until it's solved +2

THANK YOU!!!!!!! That did it!!!!!!!
One more question, when is this line needed?

myRequest.Proxy = WebRequest.DefaultWebProxy

THANK YOU!!!!!!! That did it!!!!!!!

Great!

One more question, when is this line needed?
myRequest.Proxy = WebRequest.DefaultWebProxy

If you want your web requests to go through the proxy. I've never used it since I rarely request the same page. If you want to use the proxy, take a look at WebRequest.GetSystemWebProxy which uses IE's proxy settings. IMHO pros and cons of using proxy are quite small, so you can drop that line unless you constantly request the same web page(s).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.