TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET HttpClient Example: System.Net.Http

Download a web page with HttpClient. Use the Async and Await keywords and the System.Net.Http namespace.
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.Async, Await
With asynchronous programming, we download files without interrupting other parts of our program. Many details are required to use HttpClient.
An example. Let us examine this simple program. We first add an "Imports System.Net.Http" statement at the top. In Main we create a Task.

AddressOf: We specify the Sub DownloadPageAsync with the AddressOf operator. This Sub is run when the Task starts.

AddressOf

HttpClient: We create an HttpClient instance in a Using-statement. This ensures disposal of resources.

Using

GetAsync: 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.

Substring
VB.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...
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).
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf