read webresponse stream first line returns empty string

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2007
Posts: 192
Reputation: plusplus is an unknown quantity at this point 
Solved Threads: 16
plusplus plusplus is offline Offline
Junior Poster

read webresponse stream first line returns empty string

 
0
  #1
Jun 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: read webresponse stream first line returns empty string

 
0
  #2
Jun 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 192
Reputation: plusplus is an unknown quantity at this point 
Solved Threads: 16
plusplus plusplus is offline Offline
Junior Poster

Re: read webresponse stream first line returns empty string

 
0
  #3
Jun 17th, 2009
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.
Last edited by plusplus; Jun 17th, 2009 at 6:36 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: read webresponse stream first line returns empty string

 
0
  #4
Jun 17th, 2009
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
  1. i += 1
  2. sLine = objReader.ReadLine
  3. If i = 1 AndAlso String.IsNullOrEmpty(sLine) Then
  4. Continue Do
  5. End If
Not an elegant solution but it should work
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 192
Reputation: plusplus is an unknown quantity at this point 
Solved Threads: 16
plusplus plusplus is offline Offline
Junior Poster

Re: read webresponse stream first line returns empty string

 
0
  #5
Jun 18th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 192
Reputation: plusplus is an unknown quantity at this point 
Solved Threads: 16
plusplus plusplus is offline Offline
Junior Poster

Re: read webresponse stream first line returns empty string

 
0
  #6
Jun 18th, 2009
Another thing, that seemed strange, was that I couldn't use .readtoend, somehow it only gave me an empty string.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: read webresponse stream first line returns empty string

 
0
  #7
Jun 18th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: read webresponse stream first line returns empty string

 
1
  #8
Jun 18th, 2009
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
  1. Dim myRequest As WebRequest = WebRequest.Create("http://127.0.0.1/test.txt")
  2. myRequest.Proxy = WebRequest.DefaultWebProxy
  3. Dim mystream As Stream = myRequest.GetResponse.GetResponseStream()
  4. Dim objReader As New StreamReader(mystream)
  5.  
  6. Dim sLine As String = ""
  7. Dim i As Integer = 0
  8. ListBox1.Items.Clear() 'this is a listbox
  9. Do While Not sLine Is Nothing
  10. i += 1
  11. sLine = objReader.ReadLine
  12. If Not sLine Is Nothing Then
  13. sLine = sLine.Replace(System.Convert.ToChar(0), "")
  14. ListBox1.Items.Add(sLine)
  15. End If
  16. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 192
Reputation: plusplus is an unknown quantity at this point 
Solved Threads: 16
plusplus plusplus is offline Offline
Junior Poster

Re: read webresponse stream first line returns empty string

 
0
  #9
Jun 18th, 2009
THANK YOU!!!!!!! That did it!!!!!!!
One more question, when is this line needed?

myRequest.Proxy = WebRequest.DefaultWebProxy
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 114
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: read webresponse stream first line returns empty string

 
0
  #10
Jun 18th, 2009
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).
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC