Export (0) Print
Expand All

WebClient.DownloadData Method

Downloads data from a resource with the specified URI.

[Visual Basic]
Public Function DownloadData( _
   ByVal address As String _
) As Byte()
[C#]
public byte[] DownloadData(
 string address
);
[C++]
public: unsigned char DownloadData(
 String* address
)  __gc[];
[JScript]
public function DownloadData(
   address : String
) : Byte[];

Parameters

address
The URI to download data from.

Return Value

A byte array containing the data downloaded from the resource specified in the address parameter.

Exceptions

Exception Type Condition
WebException The URI formed by combining BaseAddress and address is invalid.

-or-

An error occurred while downloading data.

Remarks

The DownloadData method downloads the data from the URI specified by the address parameter to a local byte array.

If the BaseAddress property is not empty, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not empty, it is appended to address.

Example

[Visual Basic, C#, C++] The following example requests data from a server and displays the data returned. It assumes that remoteUri contains a valid URI for the requested data.

[Visual Basic] 
Try
    Console.Write(ControlChars.Cr + "Please enter a Url(e.g. http://www.msn.com): ")
    Dim remoteUrl As String = Console.ReadLine()            
    ' Create a new WebClient instance.
    Dim myWebClient As New WebClient()
    ' Download the home page data.
    Console.WriteLine(("Downloading " + remoteUrl))
    ' DownloadData() method takes a 'uriRemote.ToString()' and downloads the Web resource and saves it into a data buffer.
    Dim myDatabuffer As Byte() = myWebClient.DownloadData(remoteUrl)

    ' Display the downloaded data.
Dim download as String = Encoding.ASCII.GetString(myDataBuffer)
    Console.WriteLine(download)

    Console.WriteLine("Download successful.")

[C#] 
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Download home page data.
Console.WriteLine("Downloading " + remoteUri);                        
// Download the Web resource and save it into a data buffer.
byte[] myDataBuffer = myWebClient.DownloadData (remoteUri);

// Display the downloaded data.
string download = Encoding.ASCII.GetString(myDataBuffer);
Console.WriteLine(download);
                    
Console.WriteLine("Download successful.");

[C++] 
// Create a new WebClient instance.
WebClient* myWebClient = new WebClient();
// Download home page data.
Console::WriteLine(S"Downloading {0}", remoteUri);
// Download the Web resource and save it into a data buffer.
Byte myDataBuffer[] = myWebClient->DownloadData (remoteUri);

// Display the downloaded data.
String* download = Encoding::ASCII->GetString(myDataBuffer);
Console::WriteLine(download);

Console::WriteLine(S"Download successful.");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

WebClient Class | WebClient Members | System.Net Namespace

Show:
© 2014 Microsoft