C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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
Note: The DownloadString function behaves similarly to the DownloadData function, but instead of a byte array it returns a String.
StringsVB.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>
Methods:
DownloadDataAsync
DownloadStringAsync
OpenReadAsync
OpenWriteAsync
UploadDataAsync
UploadFileAsync
UploadStringAsync
UploadValuesAsync