vb.netwebview2betfair

Downloading an XML page using Webview2


I'm trying to download and extract a XML page from Betfair using Webview2

It navigates to the page ok but ignores anything I try and do with the 'result' variable, such as display it in a MsgBox, the code continues so its not freezing.

 Private Async Function Process_XML_WebView2(ByVal marketId As String) As Task
        Dim url As String = "https://www.betfair.com/www/sports/exchange/readonly/v1/bymarket?_ak=nzIFcwyWhrlwYMrh&currencyCode=GBP&locale=en_GB&marketIds=" & marketId & "&rollupLimit=2&rollupModel=STAKE&types=MARKET_STATE,RUNNER_STATE,RUNNER_EXCHANGE_PRICES_BEST,RUNNER_DESCRIPTION"

        Dim xmlData As String = Await Download_XML_WebView2(url)
        MsgBox("Start Extract")
        Extract_XML_V3(xmlData, marketId)
    End Function

    Private Async Function Download_XML_WebView2(ByVal url As String) As Task(Of String)
        Dim pageSource As String = ""

        Dim navigationTaskCompletionSource As New TaskCompletionSource(Of Boolean)()
        AddHandler webView.NavigationCompleted, Sub(sender As Object, e As CoreWebView2NavigationCompletedEventArgs)
                                                    navigationTaskCompletionSource.TrySetResult(True)
                                                End Sub

        webView.Source = New Uri(url)
        Await navigationTaskCompletionSource.Task

        Dim script As String = "document.documentElement.outerHTML"
        Dim result As String = Await webView.CoreWebView2.ExecuteScriptAsync(script).ConfigureAwait(False)

        pageSource = result
        MsgBox(pageSource)
        Return pageSource
    End Function

Solution

  • You can use the HttpClient.GetStringAsync Method to do this:

    Private Async Function Process_XML_WebView2(ByVal marketId As String) As Task
    
        Dim url As String = "https://www.betfair.com/www/sports/exchange/readonly/v1/bymarket?_ak=nzIFcwyWhrlwYMrh&currencyCode=GBP&locale=en_GB&marketIds=" & marketId & "&rollupLimit=2&rollupModel=STAKE&types=MARKET_STATE,RUNNER_STATE,RUNNER_EXCHANGE_PRICES_BEST,RUNNER_DESCRIPTION"
        Dim pageSource As String = Await New HttpClient().GetStringAsync(url)
    
        MsgBox("Start Extract")
        Extract_XML_V3(pageSource, marketId)
    
    End Function
    

    pageSource will contain the page's HTML.