|
HTML clipboard
WebClient Client = new WebClient ();
Client.DownloadFile("http:
More commonly, your application will want to process the data retrieved from
the web site. In order to do this, you use the OpenRead () method, Which returns
a stream reference. You can then simply retrieve the data from the stream.
WebClient Client = new WebClient ();
Stream strm = Client.OpenRead ("http:
The following code will demonstrate the WebClient.OpenRead () method.
In this case we will simply display the contents of the downloaded data in list
box.
We create the project as standard Windows C# application, and a list box called
listbox1, in which we will display the contents of the downloaded file. We make
the following changes to the constructor of the main form.
public form1()
{
InitializeComponent();
System.Net.WebClient Client = new WebClient();
Stream strm = Client.OpenRead("http:
new StreamReader(strm);
string line;
do
{
line = sr.ReadLine();
listbox1.Items.Add(line);
}
while (line !=null);
strm.Close();
}
StreamReader sr =
Updating Files
The webClient class also features UploadFile() and UploadData() methods. The
diffrence between them is that UploadFile() uploads a specified file given the
file name, While UploadData() uploads binary data, which is supplied as an array
of bytes:
WebClient Client = new WebClient();
Client.UploadFile("http:byte [] image;
"c:\wesiteFiles\newfile.aspx");
client.UploadData("http:
|