Results 1 to 12 of 12

Thread: [2005] DownloadStringAsync - How?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    42

    Question [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.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Dim strPath As String = Me.TextBox1.Text   'URL as string
    2.         Dim wc As New WebClient
    3.         Me.RichTextBox1.Text = wc.DownloadString(strPath)  'display in RTB
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    42

    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.

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] DownloadStringAsync - How?

    Here you go. Adapt to suit your needs:

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         Dim wc As New WebClient
    4.         '  Specify that you get alerted
    5.         '  when the download completes.
    6.         AddHandler wc.DownloadStringCompleted, AddressOf AlertStringDownloaded
    7.  
    8.         Dim uri As New Uri("http:\\www.bbc.co.uk")  'Pass the URL to here. This is just an example
    9.         wc.DownloadStringAsync(uri)
    10.  
    11.     End Sub
    12.  
    13.     Public Shared Sub AlertStringDownloaded(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
    14.  
    15.         '  If the string request went as planned and wasn't cancelled:
    16.         If e.Cancelled = False AndAlso e.Error Is Nothing Then
    17.  
    18.             Dim myString As String = CStr(e.Result)   'Use e.Result to get the String
    19.             MessageBox.Show(myString)
    20.         End If
    21.  
    22.     End Sub

    EDIT: Or you can combine the lines for declaring the Download and the URI:

    vb Code:
    1. wc.DownloadStringAsync(New Uri("http:\\www.bbc.co.uk"))
    Last edited by stimbo; Apr 22nd, 2007 at 04:02 PM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    42

    Re: [2005] DownloadStringAsync - How?

    Do you format the definition of the DocumentProgressChanged handler/event the same way?

    Thanks!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DownloadStringAsync - How?

    As I always post, always read the documentation. The documentation for the WebClient.DownloadProgressChanged event has a code example.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    42

    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.

  8. #8
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] DownloadStringAsync - How?

    It will work fine. Just have your array of Strings and pass them through or whichever way you intended.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    42

    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:
    1. Private Sub Foo()
    2.  
    3.       Dim strURL As String                'Variable to hold URL's
    4.       Dim strResult As String             'Downloaded HTML
    5.       Dim wcWebClient As New WebClient    'WebClient
    6.       Dim uriURL As New Uri(strURL)       'URI
    7.  
    8.       ' Setup Event Handlers
    9.       AddHandler wcWebClient.DownloadStringCompleted, AddressOf wcDownloadStringCompleted
    10.       AddHandler wcWebClient.DownloadProgressChanged, AddressOf wcDownloadProgressChanged
    11.  
    12.       'Get next URL
    13.       strURL = GetNextURL()
    14.       uriURL = strURL   'Will this work???
    15.  
    16.       'Fetch HTML page
    17.       wcWebClient.DownloadStringAsync(uriURL)
    18.  
    19.       'check wcWebClient.DownloadProgressChanged for status
    20.  
    21.       'check wcWebClient.DownloadStringCompleted to know when its done
    22.  
    23.  
    24.    End Sub
    25.    Public Shared Sub weDownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    26.       ' Get status during download here
    27.  
    28.    End Sub
    29.  
    30.    Public Shared Sub wcDownloadStringCompleted(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
    31.       'If the string request went as planned and wasn't cancelled:
    32.       If e.Cancelled = False AndAlso e.Error Is Nothing Then
    33.          'Use e.Result to get the String
    34.          Dim myString As String = CStr(e.Result)
    35.          MessageBox.Show(myString)
    36.       End If
    37.    End Sub
    Last edited by rock-n-glock; Apr 23rd, 2007 at 07:23 PM.

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    42

    Re: [2005] DownloadStringAsync - How?

    That still doesn't work, still have my original problem. How do you convert a String into a URI?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DownloadStringAsync - How?

    First of all you have a String variable that is Nothing:
    vb Code:
    1. Dim strURL As String
    which you then create a new Uri object with:
    vb Code:
    1. 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:
    1. strURL = GetNextURL()
    2. 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:
    1. Dim strURL As String
    2. '...
    3. Dim uriURL As Uri
    4. '...
    5. strURL = GetNextURL()
    6. uriURL = New Uri(strURL)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    42

    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
  •  



Click Here to Expand Forum to Full Width