C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
AddressOf: We specify the Sub DownloadPageAsync with the AddressOf operator. This Sub is run when the Task starts.
AddressOfHttpClient: We create an HttpClient instance in a Using-statement. This ensures disposal of resources.
UsingGetAsync: We use GetAsync, Content and ReadAsStringAsync to download the web file with HttpClient.
Result: The program will display the first 50 characters in a text version of a Wikipedia page.
SubstringVB.NET program that uses HttpClient
Imports System.Net.Http
Module Module1
Sub Main()
' Create new Task.
' ... Use AddressOf to reference a method.
Dim t As Task = New Task(AddressOf DownloadPageAsync)
' Start the task.
t.Start()
' Print a message as the page downloads.
Console.WriteLine("Downloading page...")
Console.ReadLine()
End Sub
Async Sub DownloadPageAsync()
Dim page As String = "http://en.wikipedia.org/"
' Use HttpClient in Using-statement.
' ... Use GetAsync to get the page data.
Using client As HttpClient = New HttpClient()
Using response As HttpResponseMessage = Await client.GetAsync(page)
Using content As HttpContent = response.Content
' Get contents of page as a String.
Dim result As String = Await content.ReadAsStringAsync()
' If data exists, print a substring.
If result IsNot Nothing And result.Length > 50 Then
Console.WriteLine(result.Substring(0, 50) + "...")
End If
End Using
End Using
End Using
End Sub
End Module
Output
Downloading page...
<!DOCTYPE html>
<html lang="en" dir="ltr" class="c...