[TUT] Reading lines from a web document.
Well this isn't so much a tut as a quick snippet. It's a pretty handy function. I'll do my best to explain what it does within the code by annotating.
I'm not sure if this is the most effective way of doing it but yeah, I just figured this out myself 'cos nothing was really working. Pretty simple to follow but very useful. I used this extensively in my MPGH.net download and multi-tool (both got closed 'cos they were too epic)
anywho,
here's the function
[php]
Function ReadLiner(ByVal Pageurl As String)
Dim Lines As String = ""
Try
Dim request As HttpWebRequest = WebRequest.Create(Pageurl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
Lines = reader.ReadLine 'reads the current line'
Do While (Not Lines Is Nothing) 'will do the following for every line until it encounters an empty line, do whatever you want here'
'do something here, ill put an example underneath'
Console.WriteLine(Lines)
Lines = reader.ReadLine
Loop
End Using
Catch ex As Exception
'if the URL isnt found, what will you do?'
End Try
Return Lines
End Function
[/php]
Then to use:
[php]
ReadLiner("http://example.com/test.txt") ' your URL goes there'
[/php]
examples of use:
1. Add each line to a listbox.
[php]
Function ReadLiner(ByVal Pageurl As String)
Dim Lines As String = ""
Try
Dim request As HttpWebRequest = WebRequest.Create(Pageurl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
Lines = reader.ReadLine 'reads the current line'
Do While (Not Lines Is Nothing)
Listbox1.Items.Add(Lines)
Console.WriteLine(Lines)
Lines = reader.ReadLine
Loop
End Using
Catch ex As Exception
End Try
Return Lines
End Function
[/php]
2. Incorporating cases (only realistic with a few cases, I used something similar in my downloader with 4 cases -.-)
[php]
Function ReadLiner(ByVal Pageurl As String)
Dim i as Integer = 0
Dim Lines As String = ""
Try
Dim request As HttpWebRequest = WebRequest.Create(Pageurl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
Lines = reader.ReadLine 'reads the current line'
Do While (Not Lines Is Nothing)
i += 1 '(this adds 1 to the integer 'i' every new line)'
Select Case(i)
Case1
'Do what you want for the first line for example'
Label1.Text = Line
Case2
'what do you want to do with the 2nd line..'
'etc etc etc, add more cases as the need arises'
End Select
Console.WriteLine(Lines)
Lines = reader.ReadLine
Loop
End Using
Catch ex As Exception
'if the URL isnt found, what will you do?'
End Try
Return Lines
End Function
[/php]
And yeah that's about it for reading each line of a document. Hope someone gets something out of this.
J-Deezy
I'm not sure if this is the most effective way of doing it but yeah, I just figured this out myself 'cos nothing was really working. Pretty simple to follow but very useful. I used this extensively in my MPGH.net download and multi-tool (both got closed 'cos they were too epic)
anywho,
here's the function
[php]
Function ReadLiner(ByVal Pageurl As String)
Dim Lines As String = ""
Try
Dim request As HttpWebRequest = WebRequest.Create(Pageurl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
Lines = reader.ReadLine 'reads the current line'
Do While (Not Lines Is Nothing) 'will do the following for every line until it encounters an empty line, do whatever you want here'
'do something here, ill put an example underneath'
Console.WriteLine(Lines)
Lines = reader.ReadLine
Loop
End Using
Catch ex As Exception
'if the URL isnt found, what will you do?'
End Try
Return Lines
End Function
[/php]
Then to use:
[php]
ReadLiner("http://example.com/test.txt") ' your URL goes there'
[/php]
examples of use:
1. Add each line to a listbox.
[php]
Function ReadLiner(ByVal Pageurl As String)
Dim Lines As String = ""
Try
Dim request As HttpWebRequest = WebRequest.Create(Pageurl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
Lines = reader.ReadLine 'reads the current line'
Do While (Not Lines Is Nothing)
Listbox1.Items.Add(Lines)
Console.WriteLine(Lines)
Lines = reader.ReadLine
Loop
End Using
Catch ex As Exception
End Try
Return Lines
End Function
[/php]
2. Incorporating cases (only realistic with a few cases, I used something similar in my downloader with 4 cases -.-)
[php]
Function ReadLiner(ByVal Pageurl As String)
Dim i as Integer = 0
Dim Lines As String = ""
Try
Dim request As HttpWebRequest = WebRequest.Create(Pageurl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
Lines = reader.ReadLine 'reads the current line'
Do While (Not Lines Is Nothing)
i += 1 '(this adds 1 to the integer 'i' every new line)'
Select Case(i)
Case1
'Do what you want for the first line for example'
Label1.Text = Line
Case2
'what do you want to do with the 2nd line..'
'etc etc etc, add more cases as the need arises'
End Select
Console.WriteLine(Lines)
Lines = reader.ReadLine
Loop
End Using
Catch ex As Exception
'if the URL isnt found, what will you do?'
End Try
Return Lines
End Function
[/php]
And yeah that's about it for reading each line of a document. Hope someone gets something out of this.
J-Deezy





, I be but a humble choob next to the awesome might of your VB knowledge. Minion this douche now plocks.