HttpClient. A network access takes some time to finish. This causes a pause in our program's execution. With Async and Await we use HttpClient to download pages in a better way.
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 ModuleDownloading page...
<!DOCTYPE html>
<html lang="en" dir="ltr" class="c...
Some notes. HttpClient is a more advanced class, and was added later, than other classes like WebClient. This class is built for asynchronous use.
For downloading web pages, it is better to enable and support GZIP compression. Another header can be added to HttpClient. We can use VB.NET to decompress these files.
A review. HttpClient is a powerful class. And it is an effective way to download web pages and other files through HTTP (a protocol).
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 29, 2023 (edit).