December 2007
Intermediate to advanced
896 pages
19h 57m
English
You need to download data from or upload data to a location specified by a URL; this data can be either an array of bytes or a file.
Use the WebClient.UploadData or WebClient.DownloadData methods to transfer data using a URL.
To download the data for a web page, do the following:
Uri uri = new Uri("http://localhost:4088/CSCBWeb/DownloadData16_4.aspx");
// make a client
using (WebClient client = new WebClient( ))
{
// get the contents of the file
Console.WriteLine("Downloading {0} ",uri.AbsoluteUri);
// download the page and store the bytes
byte[] bytes;
try
{
bytes = client.DownloadData(uri);
}
catch (WebException we)
{
Console.WriteLine(we.ToString( ));
return;
}
// Write the content out
string page = Encoding.ASCII.GetString(bytes);
Console.WriteLine(page);
}This will produce the following output:
Downloading http://localhost:4088/CSCBWeb/DownloadData16_4.aspx <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or g/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> Untitled Page </title></head> <body> <form name="Form1" method="post" action="DownloadData16_4.aspx" id="Form2"> <input type="hidden" name="_ _VIEWSTATE" value="dDwyMDQwNjUzNDY2Ozs+kS9hguYm9369sybDqmIow0AvxBg=" /> <span id="Label1" style="Z-INDEX: 101; LEFT: 142px; POSITION: absolute; TOP: 164px">This is downloaded html!</span> </form> </body> </html>
You can also download data to a file using DownloadFile ...
Read now
Unlock full access