|
-
Dec 24th, 2009, 03:13 AM
#1
Thread Starter
Addicted Member
[RESOLVED] DownloadStringASync-Somethings wrong
Hi, This is an old code I have dug up, and I do remember it working, at least I think it was this exact code. Anyways, I'm trying to download a WebPage as a String, which works, however, I can not parse each line of Html at a time. I can either, Parse each Character, or parse the whole thing as a single String. Here's my code:
Code:
Public Class Form1
Private WithEvents WC As New Net.WebClient
Private Sub WC_StringFinished(ByVal sender As System.Object, ByVal e As Net.DownloadStringCompletedEventArgs) Handles WC.DownloadStringCompleted
Dim htmlLines() As String = e.Result.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
For Each i As String In htmlLines
ComboBox1.Items.Add(i)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WC.DownloadStringAsync(New Uri("http://www.google.ca/search?hl=en&q=Mal1t1a&btnG=Google+Search&meta=&aq=f&oq="))
End Sub
End Class
-
Dec 24th, 2009, 06:56 AM
#2
Re: DownloadStringASync-Somethings wrong
Are you sure that the lines are delimited by the value you're specifying? Environment.NewLine will return a carriage return and line feed pair on a Windows system, i.e. CR LF. Quite possibly the file you're reading has lines delimited by line feeds only. If you want to be able to handle both, try creating a StringReader and calling ReadLine.
-
Dec 25th, 2009, 06:08 PM
#3
Thread Starter
Addicted Member
Re: DownloadStringASync-Somethings wrong
Well, all I'm trying to accomplish, is the read the HTML Page, line-by-line. It's worked before, and well, now it doesn't.
-
Dec 25th, 2009, 06:12 PM
#4
Fanatic Member
Re: DownloadStringASync-Somethings wrong
Create a StringReader, as jmcilhinney said.
That way you can read the code line by line.
And are you sure that the StringFinished event is called when the download is complete?
Try adding a MsgBox saying something to be sure that the event gets raised.
-
Dec 25th, 2009, 06:13 PM
#5
Thread Starter
Addicted Member
Re: DownloadStringASync-Somethings wrong
Yes, I'm positive it's being called. I can try creating a StringReader as he said, however, it feels odd, because I've never needed to do that before. I'm pretty sure this code has sufficed for me.
-
Dec 25th, 2009, 07:51 PM
#6
Re: DownloadStringASync-Somethings wrong
It doesn't matter what you're trying to accomplish; if you don't use the right code then it won't work. The fact that some code worked for one file doesn't necessarily mean it will work for another file because the two files may be different. As I said in my previous post, you are explicitly try to split the file on CR/LF pairs. If the lines of the file are delimited by LF characters alone then there are no CR/LF pairs so the file won't be split. You can test that by trying to split that file on LF characters alone:
Code:
Dim htmlLines() As String = e.Result.Split(ControlChars.Lf)
If that works where your other code doesn't then that is the issue for that particular file.
As I also said in my previous post, if you want to be able to handle files that are delimited in both ways then you should use a StringReader.
Last edited by jmcilhinney; Dec 25th, 2009 at 08:24 PM.
-
Dec 26th, 2009, 04:12 PM
#7
Thread Starter
Addicted Member
Re: DownloadStringASync-Somethings wrong
 Originally Posted by jmcilhinney
It doesn't matter what you're trying to accomplish; if you don't use the right code then it won't work. The fact that some code worked for one file doesn't necessarily mean it will work for another file because the two files may be different. As I said in my previous post, you are explicitly try to split the file on CR/LF pairs. If the lines of the file are delimited by LF characters alone then there are no CR/LF pairs so the file won't be split. You can test that by trying to split that file on LF characters alone:
Code:
Dim htmlLines() As String = e.Result.Split(ControlChars.Lf)
If that works where your other code doesn't then that is the issue for that particular file.
As I also said in my previous post, if you want to be able to handle files that are delimited in both ways then you should use a StringReader.
Thank-you, That worked perfectly, also, sorry, I had to Re-Download and Install Everything. I don't exactly understand how to tell. Would I just try both of them? Or is there a certain way to actually tell? Also, thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|