[Help] Problem reading text from a webpage

Posts 1–15 of 19 · Page 1 of 2
[Help] Problem reading text from a webpage
Hey guys. I'm trying to use StreamReader to read all the text in a .txt document I uploaded on my ftp site...

I'm currently using this to read 4 different text documents and print them into a label...

[php]
Dim url1, url2, url3, url4 As String
url1 = "http://mywebsite/TextDocument1.txt"
url2 = "http://mywebsite/TextDocument2.txt"
url3 = "http://mywebsite/TextDocument3.txt"
url4 = "http://mywebsite/TextDocument4.txt"

Dim request1, request2, request3, request4 As System.Net.HttpWebRequest

request1 = System.Net.HttpWebRequest.Create(url1)
request2 = System.Net.HttpWebRequest.Create(url2)
request3 = System.Net.HttpWebRequest.Create(url3)
request4 = System.Net.HttpWebRequest.Create(url4)

Dim response1, response2, response3, response4 As System.Net.HttpWebResponse
response1 = request1.GetResponse()
response2 = request2.GetResponse()
response3 = request3.GetResponse()
response4 = request4.GetResponse()

Dim sr1, sr2, sr3, sr4 As System.IO.StreamReader

sr1 = New System.IO.StreamReader(response1.GetResponseStream (), System.Text.Encoding.GetEncoding("windows-1252"))
sr2 = New System.IO.StreamReader(response2.GetResponseStream (), System.Text.Encoding.GetEncoding("windows-1252"))
sr3 = New System.IO.StreamReader(response3.GetResponseStream (), System.Text.Encoding.GetEncoding("windows-1252"))
sr4 = New System.IO.StreamReader(response4.GetResponseStream (), System.Text.Encoding.GetEncoding("windows-1252"))

Dim Online1, Online2, Online3, Online4 As String
Online1 = sr1.ReadToEnd()
Online2 = sr2.ReadToEnd()
Online3 = sr3.ReadToEnd()
Online4 = sr4.ReadToEnd()

Label2.Text = ("1. " + Online1)
Label3.Text = ("2. " + Online2)
Label4.Text = ("3. " + Online3)
Label5.Text = ("4. " + Online3)

[/php]

Only trouble is, nothing happens O.o

Any glaring errors in my code?
First, use this function:

Code:
 
Function GetPage(ByVal pageUrl As String) As String
	  Dim s As String = ""
	  Try
		 Dim request As HttpWebRequest = WebRequest.Create(pageUrl)
		 Dim response As HttpWebResponse = request.GetResponse()
		 Using reader As StreamReader = New StreamReader(response.GetResponseStream())
			s = reader.ReadToEnd()
		 End Using
	  Catch ex As Exception
		 Debug.WriteLine("FAIL: " + ex.Message)
	  End Try
	  Return s
   End Function
Then, Try this.

Code:
Dim url1, url2, url3, url4 As String
        url1 = "http://mywebsite/TextDocument1.txt"
        url2 = "http://mywebsite/TextDocument2.txt"
        url3 = "http://mywebsite/TextDocument3.txt"
        url4 = "http://mywebsite/TextDocument4.txt"
Dim Online1, Online2, Online3, Online4 As String 
Online1 = GetPage(url1)
Online2 = GetPage(url2)
Online3 = GetPage(url3)
Online4 = GetPage(url4)
Quote Originally Posted by Hassan View Post
First, use this function:

Code:
 
Function GetPage(ByVal pageUrl As String) As String
	  Dim s As String = ""
	  Try
		 Dim request As HttpWebRequest = WebRequest.Create(pageUrl)
		 Dim response As HttpWebResponse = request.GetResponse()
		 Using reader As StreamReader = New StreamReader(response.GetResponseStream())
			s = reader.ReadToEnd()
		 End Using
	  Catch ex As Exception
		 Debug.WriteLine("FAIL: " + ex.Message)
	  End Try
	  Return s
   End Function
Then, Try this.

Code:
Dim url1, url2, url3, url4 As String
        url1 = "http://mywebsite/TextDocument1.txt"
        url2 = "http://mywebsite/TextDocument2.txt"
        url3 = "http://mywebsite/TextDocument3.txt"
        url4 = "http://mywebsite/TextDocument4.txt"
Dim Online1, Online2, Online3, Online4 As String 
Online1 = GetPage(url1)
Online2 = GetPage(url2)
Online3 = GetPage(url3)
Online4 = GetPage(url4)
Can I have your children?

Thanks so much man, works beautifully and so much less messy Thanks.
Quote Originally Posted by Jason View Post
Can I have your children?

Thanks so much man, works beautifully and so much less messy Thanks.
No, I love my children...lol

No problem...*cough* thnx !! ?
xD
Quote Originally Posted by Hassan View Post
No, I love my children...lol

No problem...*cough* thnx !! ?
xD
I meant can I make you some more silly

I pressed thanks!
Quote Originally Posted by Jason View Post
I meant can I make you some more silly

I pressed thanks!
No, you can't...and thnx
Oh wow, and thanks. haha solved tag anyone?
Hey Flame if you happen to look at this thread again, is it possible to read one line, save it to a string variable, read the next line, save it to another string variable...etc
Code:
While Not reader.EndOfStream

            Dim s As String = reader.ReadLine
           
        End While
You may do something like...

Code:
if s.startswith("abc")...
Code:
if s.contains("def")...
I don't know how to declare a new variable for each line. You may use a hidden listbox or idk...tell me exactly why/how you want to do that ^.-
Well basically what I want to do is have a few things associated to a general topic that I save in a text document.

i.e my text document is set out like this

[php]
NAME
LINK
LINK2
[/php]

But I want to read the line that contains the line and assign it to a label
then I want to read second line and assign that to another string variable

I.e (this isn't actual code obviously but I hope it helps you understand better what i mean)

[php]

Dim DocName, DocLink1, DocLink2 as string
Dim request As HttpWebRequest = WebRequest.Create(pageUrl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())

DocName = reader.ReadLine1
DocLink1 = reader.ReadLine2
DocLink2 = reader.ReadLine3

end using

[/php]

But you can't tell it what line to read :O
Quote Originally Posted by Blubb1337 View Post
One second....

........one
Fu.

Add the following function:

Code:
  Public Shared Function ReadSpecifiedLine(ByVal file As String, ByVal lineNum As Integer) As String
        'create a variable to hold the contents of the file
        Dim contents As String = String.Empty
        'always use a try...catch to deal
        'with any exceptions that may occur
        Try
            Using stream As New StreamReader(file)
                contents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
                Dim linesArray As String() = contents.Split(New Char() {ControlChars.Lf})

                'Make sure we have ana ctual array
                If linesArray.Length > 1 Then
                    'Make sure user didnt provide number greater than the number
                    'of lines in the array, and not less than 0 (zero) Thanks AdamSpeight2008
                    If Not lineNum > linesArray.Length AndAlso Not lineNum < 0 Then
                        Return linesArray(lineNum)
                    Else
                        'Failed our check so return the first line in the array
                        Return linesArray(0)
                    End If
                Else
                    'No array so return the line
                    Return contents
                End If
            End Using
        Catch ex As Exception
            Return ex.ToString()
        End Try
    End Function
Code:
Dim s as string = reader.readtoend
Code:
DocName = ReadSpecifiedLine(s, 0)         
            DocLink1 = ReadSpecifiedLine(s, 1)
            DocLink2 = ReadSpecifiedLine(s, 2)
Eating, fu again.
Quote Originally Posted by Blubb1337 View Post
Fu.

Add the following function:

Code:
  Public Shared Function ReadSpecifiedLine(ByVal file As String, ByVal lineNum As Integer) As String
        'create a variable to hold the contents of the file
        Dim contents As String = String.Empty
        'always use a try...catch to deal
        'with any exceptions that may occur
        Try
            Using stream As New StreamReader(file)
                contents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
                Dim linesArray As String() = contents.Split(New Char() {ControlChars.Lf})

                'Make sure we have ana ctual array
                If linesArray.Length > 1 Then
                    'Make sure user didnt provide number greater than the number
                    'of lines in the array, and not less than 0 (zero) Thanks AdamSpeight2008
                    If Not lineNum > linesArray.Length AndAlso Not lineNum < 0 Then
                        Return linesArray(lineNum)
                    Else
                        'Failed our check so return the first line in the array
                        Return linesArray(0)
                    End If
                Else
                    'No array so return the line
                    Return contents
                End If
            End Using
        Catch ex As Exception
            Return ex.ToString()
        End Try
    End Function
Code:
Dim s as string = reader.readtoend
Code:
DocName = ReadSpecifiedLine(s, 0)         
            DocLink1 = ReadSpecifiedLine(s, 1)
            DocLink2 = ReadSpecifiedLine(s, 2)
Eating, fu again.
hehehe thanks Blubb, I'll go checkit out now! My back is like crippled so im bored as fuck
Code something for me, no I don't know what x_X.

U no onlaine @ msn?
Posts 1–15 of 19 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?