HomeSearch

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.dotnetperls.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.dotnetperls.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.dotnetperls.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.
Home
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.