| | |
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:
Solved Threads: 16
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
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
I tried your code with
Have you tried other URLs? What if you comment out
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
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
Teme64 @ Windows Developer Blog
Ok. I tried in my localhost
I also asked, have you tried other URLs? Like
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 Not an elegant solution but it should work
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)
i += 1 sLine = objReader.ReadLine If i = 1 AndAlso String.IsNullOrEmpty(sLine) Then Continue Do End If
Teme64 @ Windows Developer Blog
•
•
Join Date: Jul 2007
Posts: 192
Reputation:
Solved Threads: 16
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
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
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
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
Teme64 @ Windows Developer Blog
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
The line
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)
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
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. Teme64 @ Windows Developer Blog
•
•
•
•
THANK YOU!!!!!!! That did it!!!!!!!
•
•
•
•
One more question, when is this line needed?
myRequest.Proxy = WebRequest.DefaultWebProxy
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Using split with a line break (Java)
- reading txt doc (Java)
- scanf() to read in a string... (C)
- Reading ints from a file and creating a list with them. (Python)
- Scanning a string? (Python)
- Please help: Unable to read beyond the end of the stream (VB.NET)
- Need help quick (C)
- need suggestion on this project (Visual Basic 4 / 5 / 6)
- TKinter Beginner (Python)
Other Threads in the VB.NET Forum
- Previous Thread: Dynamically adding buttons
- Next Thread: Tabs controls
| Thread Tools | Search this Thread |
.net .net2005 30minutes 2005 2008 access account arithmetic array basic binary bing button buttons c# center check code combobox component connectionstring convert crystalreport data database databasesearch datagrid datagridview design dissertation dissertations dissertationthesis dropdownlist excel file-dialog folder ftp generatetags google gridview hardcopy image images inline insert intel internet listview mobile monitor ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project reports" save savedialog searchbox searchvb.net select serial server soap sql table tcp text textbox timer toolbox trim update updown user usercontrol vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





