[TUT] Reading lines from a web document.

Posts 1–11 of 11 · Page 1 of 1
[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
Nice tutorial !! Thanks for sharing..
j-dezzy awesomnness aggaiN!
Quote Originally Posted by /b/oss View Post
j-dezzy awesomnness aggaiN!
J-Deezy ** but thanks.
Quote Originally Posted by Jason View Post
J-Deezy ** but thanks.
NOT A SPAMMER!!! YAY!! finally, nice one J-Deezy.
Quote Originally Posted by 'Bruno View Post
NOT A SPAMMER!!! YAY!! finally, nice one J-Deezy.
Hahaha maybe I should incorporate it into a spammer. I'm watching NCIS right now so im aircoding right here

No C&P, I don't have VB on this computer, wonder how many syntax errors i get

You ned (for this fictional piece of shit)

TextBox1 is the delay
Button1 is to fill the listbox from the website specified in TextBox2
TextBox2 is the webpage to read from
Button2 is to start spam
Button3 is to stop spam
lblStatus states the status (spamming/idle)

FUCK YEAH!

[php]
Imports System.NET
Imports System.IO

Public Class Form1

Private Function Reade(ByVal pages As String)

dim lines as string = ""

Try
Dim request As HttpWebRequest = WebRequest.Create(Pages)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())

lines = reader.readline

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

Private Sub Button1_click(ByVal...yeah shit goes here im not that good) Handles Button1.click

'summary: This button fills the listbox with the text'
If not TextBox2.Text = "" then
try
reade(TextBox2.Text)
catch ex as Exception
End try

Else: MsgBox("Please enter a website to load from", vbinformation, "No website specified")


End Sub

Private Sub randomline()
'this generates a random listbox entry'
dim randoms as new random
dim x as integer = ListBox1.Items.Count
dim i as integer = randoms.next(0, x)

ListBox1.SelectedIndex = i

End Sub

Private sub Timer1_tick(ByVal....) Handles Timer1.Tick
'spams a random listbox entry'
call randomline()
SendKeys.Send(Listbox1.SelectedItem)
SendKeys.Send("{ENTER}")

End Sub

Private Sub Button2_click(ByVal....) Handles Button2.Click
'the start spam button'
if not textbox1.text = "" then
Timer1.Interval = Textbox1.Text 'oh yeah customizable delay ^^'
Else: Timer1.Interval = 200
End If

Timer1.Start()
lblStatus = "Spamming"
lblStatus.ForeColor = Color.Lime

End Sub

Private Sub Button3_click(Byval....)Handles Button3.Click
'the stop spam button'

Timer1.Stop()
lblStatus = "Idle"
lblStatus.ForeColor = Color.Red

End Sub

End Class

[/php]

Air code over and out
Not hard, but very good job. A lot of people don't know how to do such.

Tip: Instead of rewriting a long code everytime and changing just a bit, simply do one function (see above)
Wow I'm a genius, my above code worked flawlessly.
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?