943,544 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2187
  • VB.NET RSS
Jun 16th, 2009
0

read webresponse stream first line returns empty string

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 16
Posting Whiz in Training
plusplus is offline Offline
207 posts
since Jul 2007
Jun 16th, 2009
0

Re: read webresponse stream first line returns empty string

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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 17th, 2009
0

Re: read webresponse stream first line returns empty string

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.
Reputation Points: 10
Solved Threads: 16
Posting Whiz in Training
plusplus is offline Offline
207 posts
since Jul 2007
Jun 17th, 2009
0

Re: read webresponse stream first line returns empty string

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
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 18th, 2009
0

Re: read webresponse stream first line returns empty string

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
Reputation Points: 10
Solved Threads: 16
Posting Whiz in Training
plusplus is offline Offline
207 posts
since Jul 2007
Jun 18th, 2009
0

Re: read webresponse stream first line returns empty string

Another thing, that seemed strange, was that I couldn't use .readtoend, somehow it only gave me an empty string.
Reputation Points: 10
Solved Threads: 16
Posting Whiz in Training
plusplus is offline Offline
207 posts
since Jul 2007
Jun 18th, 2009
0

Re: read webresponse stream first line returns 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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 18th, 2009
1

Re: read webresponse stream first line returns empty string

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
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 18th, 2009
0

Re: read webresponse stream first line returns empty string

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

myRequest.Proxy = WebRequest.DefaultWebProxy
Reputation Points: 10
Solved Threads: 16
Posting Whiz in Training
plusplus is offline Offline
207 posts
since Jul 2007
Jun 18th, 2009
0

Re: read webresponse stream first line returns empty string

Quote ...
THANK YOU!!!!!!! That did it!!!!!!!
Great!

Quote ...
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).
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Dynamically adding buttons
Next Thread in VB.NET Forum Timeline: Tabs controls





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC