5 Replies - 749 Views - Last Post: 01 February 2013 - 09:57 AM Rate Topic: -----

#1 djstanley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Reading lines of GetResponseStream

Posted 31 January 2013 - 07:20 PM

Hey all,

I'm using the following to GET web pages:

Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.CookieContainer = logincookie
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim reader As New StreamReader(response.GetResponseStream())
Dim thesource As String = reader.ReadToEnd


I'm trying to extract data from the source. thesource holds the source code of the page successfully, however, I can't loop through each line using a For Each loop, as it merely responds with each character instead of each line.

Can somebody suggest how to fix this?
Thanks guys, Stan.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading lines of GetResponseStream

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5574
  • View blog
  • Posts: 11,916
  • Joined: 02-June 10

Re: Reading lines of GetResponseStream

Posted 01 February 2013 - 07:09 AM

Might help if you gave us your loop code as well; ya know, the part not working for you.
Was This Post Helpful? 1
  • +
  • -

#3 djstanley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Re: Reading lines of GetResponseStream

Posted 01 February 2013 - 07:33 AM

OK:

For each line in thesource
msgbox(line)
end loop



Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5574
  • View blog
  • Posts: 11,916
  • Joined: 02-June 10

Re: Reading lines of GetResponseStream

Posted 01 February 2013 - 08:03 AM

Ok, then that makes sense.

LIne 5 you read one line and set that into a string.

you then loop through each smaller part of said string: That would be a character. A string is just an array of char

Quote

5 Dim thesource As String = reader.ReadToEnd

For each line in thesource
   msgbox(line)
end loop



You probably want a loop around the reader.read in order to get each line. But that is just a guess as I don't know the entire content of the reader.

.ReadToEnd might get EVERYTHING (IE: Multiple lines). If it does, then you want to break it down. Maybe do a .Split() on the NewLine character for example. The loop through the resultant string[]

You should put a breakpoint right after line 5 and examine the content of theSource at runtime to make that determination
Was This Post Helpful? 0
  • +
  • -

#5 djstanley  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 13

Re: Reading lines of GetResponseStream

Posted 01 February 2013 - 09:40 AM

View PosttlhIn`toq, on 01 February 2013 - 08:03 AM, said:

Ok, then that makes sense.

LIne 5 you read one line and set that into a string.

you then loop through each smaller part of said string: That would be a character. A string is just an array of char

Quote

5 Dim thesource As String = reader.ReadToEnd

For each line in thesource
   msgbox(line)
end loop



You probably want a loop around the reader.read in order to get each line. But that is just a guess as I don't know the entire content of the reader.

.ReadToEnd might get EVERYTHING (IE: Multiple lines). If it does, then you want to break it down. Maybe do a .Split() on the NewLine character for example. The loop through the resultant string[]

You should put a breakpoint right after line 5 and examine the content of theSource at runtime to make that determination



Thankyou for the help. Unfortunatly the problem remains, even when looping around the reader.read and not even appending the contents to a string :surrender:

However, using Msgbox(reader.readline) instead of readtoend, it displays the opening tag <!DOCTYPE html> which is correct! :bananaman: But the following:

       For i = 0 To reader.ReadLine.Length
            MsgBox(reader.ReadLine(i))
        Next


Results in a few characters being displayed (like before) and then crashing.

Readline seems to be successful, perhaps I'm noobily using it wrong? Can anyone lend a hand?

Thanks,
Stan :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5574
  • View blog
  • Posts: 11,916
  • Joined: 02-June 10

Re: Reading lines of GetResponseStream

Posted 01 February 2013 - 09:57 AM

Each time you call .ReadLIne you are reading the next line.

So line 1 you call ReadLine and get the length of that line. You then don't do anything with that line.

Line 2 you then read the NEXT line which will be a string, but then display only one element of that string with the (i), which would be a char

You need to set a string equal to the line you read.
Work that string as needed.
Then get the next line

Basically this - but however you do this loop in VB.

do
{
     string temp = reader.readline; 
    // now work temp as needed
}
While (temp != null)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1