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 WebClient: DownloadData, Headers

Use WebClient to download files on the Internet. Call the DownloadData Function.
WebClient downloads files from remote servers. We use the WebClient type in the System.Net namespace. We make network requests to Internet sites or other servers with an HTTP address. WebClient provides critical functionality.
Example. To start, the WebClient type is found in the System.Net namespace and you should include this with an Imports directive at the top of your program. Next, the WebClient is best used in the scope of a Using-block.

Tip: The Using-block ensures the system will reclaim the resources as soon as possible.

Info: The example demonstrates how you can set a request header by specifying the string of its key.

DownloadData: We call the DownloadData function which receives the URL parameter and returns a byte array containing the resource's data.

VB.NET program that uses WebClient Imports System.Net Module Module1 Sub Main() ' Resource acquisition statement. Using client As New WebClient ' Set one of the headers. client.Headers("User-Agent") = "Mozilla/4.0" ' Download data as byte array. Dim arr() As Byte = client.DownloadData("http://www.dotnetCodex.com/") ' Display result. Console.WriteLine(arr.Length) End Using End Sub End Module Output 5821
Example 2. Next we set two headers, and then download the data. Then we read one of the response headers. The example shows that the target website, when sent an appropriate Accept-Encoding, returns a Content-Encoding heading that corresponds.

And: In this way, you can use the WebClient to test the web sites you develop for correctness.

VB.NET program that uses WebClient with headers Imports System.Net Module Module1 Sub Main() Using client As New WebClient ' Set one of the headers. client.Headers("User-Agent") = "Googlebot/2.1" client.Headers("Accept-Encoding") = "gzip" ' Download data. Dim arr() As Byte = client.DownloadData("http://www.dotnetCodex.com/") ' Display result. Console.WriteLine(arr.Length) Console.WriteLine(client.ResponseHeaders("Content-Encoding")) End Using End Sub End Module Output 2092 gzip
Example 3. Another important task you can do with the WebClient is download a web page as a String, not a byte array. Often, strings are more easily used in other parts of your program, making the DownloadString method more convenient.

Note: The DownloadString function behaves similarly to the DownloadData function, but instead of a byte array it returns a String.

Strings
VB.NET program that uses DownloadString Imports System.Net Module Module1 Sub Main() Using client As New WebClient ' Download the web page as a string. Dim value As String = client.DownloadString("http://www.dotnetCodex.com/") ' Write the first characters of the result. Console.WriteLine(value.Substring(0, 15)) End Using End Sub End Module Output <!doctype html>
Asynchronous. The WebClient type provides asynchronous functions. However, instead of using the Async variants in the WebClient class, you could always use a conventional threading construct such as BackgroundWorker.BackgroundWorker
Methods: DownloadDataAsync DownloadStringAsync OpenReadAsync OpenWriteAsync UploadDataAsync UploadFileAsync UploadStringAsync UploadValuesAsync
Summary. WebClient provides essential functionality for many programs. Many .NET programs use WebClient extensively for programmatic acquisition of remote resources. These tips provide a foundation for using WebClient in your VB.NET programs.
© 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