|
-
Apr 22nd, 2007, 12:42 PM
#1
Thread Starter
Member
[2005] DownloadStringAsync - How?
I need to download the HTML of a web page but don't need to display it, so I'm changing my existing (working) code from a webbrowser class to the webclient class. The problem I'm having is that the URL needs to change programmatically as I will go through a list of them.
I can't seem to get the definitions and call right. I finally seemed to get the definitions right, but I get a squiggly line under the call. Here's what I have right now:
Code:
'I'm just trying to get the URL into a string that I can
'feed the URI in code as a variable because the URL will
'change as multiple pages are processed.
Dim strPath As String = txtUrl.Text.ToString
Dim URL As New Uri(strPath)
Using wcWebClient As New System.Net.WebClient
txtSource.Text = wcWebClient.DownloadStringAsync(URL)
End Using
Like it is, I get a squiggly line under wcWebClient.DownloadStringAsync(URL). When I hover over it, the tooltip says:
"Expression does not produce a value."
If I just type the url in directly like this:
Code:
txtSource.Text = wcWebClient.DownloadStringAsync("http://www.somedomain.com/misc-page.htm")
The squiggly line changes to a different error in the tooltip:
"Value of type 'String' cannot be converted to 'System.Uri'."
What am I doing wrong? I've searched on here and there are no threads containing DownloadStringAsync. I've searched the MSDN forums and what little information that's there is all in C#. The documentation tells about it, but doesn't show an example of doing it programmatically, only a single URL, entered directly in the .DownloadStringAsync call like the bottom example above. I don't understand why mine says "Value of type 'String' cannot be converted to 'System.Uri'" when I do it the same way. Its the URI thats screwing me up, I don't know how to use it properly.
-
Apr 22nd, 2007, 02:04 PM
#2
Re: [2005] DownloadStringAsync - How?
What's the advantage of using the Async over just DownloadString? Never used it before. I take it there's a reason why you have to use it. I have tried using just DownloadString and it worked first time.
vb Code:
Dim strPath As String = Me.TextBox1.Text 'URL as string
Dim wc As New WebClient
Me.RichTextBox1.Text = wc.DownloadString(strPath) 'display in RTB
-
Apr 22nd, 2007, 02:20 PM
#3
Thread Starter
Member
Re: [2005] DownloadStringAsync - How?
Well, if you use Async, your cpu is free to do other things while the the page is downloading. Otherwise your code hangs up on the .DownloadString icon until the page is finished downloading. So you have no method of status updates other than 'starting...' and 'complete...'.
With Async, you can monitor the DownloadStringCompleted and DownloadProcessChanged events to give real status updates.
-
Apr 22nd, 2007, 03:58 PM
#4
Re: [2005] DownloadStringAsync - How?
Here you go. Adapt to suit your needs:
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim wc As New WebClient
' Specify that you get alerted
' when the download completes.
AddHandler wc.DownloadStringCompleted, AddressOf AlertStringDownloaded
Dim uri As New Uri("http:\\www.bbc.co.uk") 'Pass the URL to here. This is just an example
wc.DownloadStringAsync(uri)
End Sub
Public Shared Sub AlertStringDownloaded(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
' If the string request went as planned and wasn't cancelled:
If e.Cancelled = False AndAlso e.Error Is Nothing Then
Dim myString As String = CStr(e.Result) 'Use e.Result to get the String
MessageBox.Show(myString)
End If
End Sub
EDIT: Or you can combine the lines for declaring the Download and the URI:
vb Code:
wc.DownloadStringAsync(New Uri("http:\\www.bbc.co.uk"))
Last edited by stimbo; Apr 22nd, 2007 at 04:02 PM.
-
Apr 22nd, 2007, 05:16 PM
#5
Thread Starter
Member
Re: [2005] DownloadStringAsync - How?
Do you format the definition of the DocumentProgressChanged handler/event the same way?
Thanks!
-
Apr 22nd, 2007, 05:56 PM
#6
Re: [2005] DownloadStringAsync - How?
As I always post, always read the documentation. The documentation for the WebClient.DownloadProgressChanged event has a code example.
-
Apr 22nd, 2007, 06:27 PM
#7
Thread Starter
Member
Re: [2005] DownloadStringAsync - How?
The original issue I had was being able to setup a string variable to pass in to the URI. Will that work with the way you have it setup in line 8? Thats the thing I couldn't find any examples of, being able to pass multiple URL's in programmatically.
-
Apr 22nd, 2007, 06:39 PM
#8
Re: [2005] DownloadStringAsync - How?
It will work fine. Just have your array of Strings and pass them through or whichever way you intended.
-
Apr 23rd, 2007, 07:19 PM
#9
Thread Starter
Member
Re: [2005] DownloadStringAsync - How?
I haven't tried this yet, wanted to post it to see if you guys think it look alright. I know I need a try/catch block in it and I'll add that as soon as I determine its going to work and I'm not in left field somewhere.
vb Code:
Private Sub Foo()
Dim strURL As String 'Variable to hold URL's
Dim strResult As String 'Downloaded HTML
Dim wcWebClient As New WebClient 'WebClient
Dim uriURL As New Uri(strURL) 'URI
' Setup Event Handlers
AddHandler wcWebClient.DownloadStringCompleted, AddressOf wcDownloadStringCompleted
AddHandler wcWebClient.DownloadProgressChanged, AddressOf wcDownloadProgressChanged
'Get next URL
strURL = GetNextURL()
uriURL = strURL 'Will this work???
'Fetch HTML page
wcWebClient.DownloadStringAsync(uriURL)
'check wcWebClient.DownloadProgressChanged for status
'check wcWebClient.DownloadStringCompleted to know when its done
End Sub
Public Shared Sub weDownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
' Get status during download here
End Sub
Public Shared Sub wcDownloadStringCompleted(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
'If the string request went as planned and wasn't cancelled:
If e.Cancelled = False AndAlso e.Error Is Nothing Then
'Use e.Result to get the String
Dim myString As String = CStr(e.Result)
MessageBox.Show(myString)
End If
End Sub
Last edited by rock-n-glock; Apr 23rd, 2007 at 07:23 PM.
-
Apr 23rd, 2007, 07:49 PM
#10
Thread Starter
Member
Re: [2005] DownloadStringAsync - How?
That still doesn't work, still have my original problem. How do you convert a String into a URI?
-
Apr 23rd, 2007, 07:50 PM
#11
Re: [2005] DownloadStringAsync - How?
First of all you have a String variable that is Nothing:which you then create a new Uri object with:
vb Code:
Dim uriURL As New Uri(strURL)
There's not much point creating a Uri object with a non-existent string and it may not even be legal. Also, you simply discard that Uri object anyway when you try to assign something else to that variable:
vb Code:
strURL = GetNextURL()
uriURL = strURL
Given that strURL is type String and uriURL is type Uri, is it logical that you can assign the value of one to the other? You have to assign Uri object to a variable of type Uri. That means that AFTER strUrl gets a value, THEN you create a Uri object with it:
vb Code:
Dim strURL As String
'...
Dim uriURL As Uri
'...
strURL = GetNextURL()
uriURL = New Uri(strURL)
-
Apr 23rd, 2007, 08:08 PM
#12
Thread Starter
Member
Re: [2005] DownloadStringAsync - How?
Yea, I just figured it out. Sorry.
I saw the problem and inserted this between the wcWebClient and uriURL definition:
Code:
strURL = "http://www.theurliwant.com"
So I'll have to call that GetNextURL() before I define the URI. It makes sense now. Thanks guys.
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
|