C# WebClient WebClient
Description
WebClient WebClient
Initializes a new instance of the
WebClient class.
Syntax
WebClient.WebClient
has the following syntax.
public WebClient()
Example
The following code example creates a WebClient instance and then uses it to download data from a server and display it on the system console, to download data from a server and write it to a file, and to upload form values to a server and receive the response.
using System;// www .j a va 2s .c o m
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Collections.Specialized;
public class Example
{
public static void Main()
{
WebClient client = new WebClient();
Byte[] pageData = client.DownloadData("http://www.java2s.com");
string pageHtml = Encoding.ASCII.GetString(pageData);
Console.WriteLine(pageHtml);
client.DownloadFile("http://www.java2s.com", "index.htm");
NameValueCollection form = new NameValueCollection();
form.Add("MyName", "MyValue");
Byte[] responseData = client.UploadValues("http://www.java2s.com/form.aspx", form);
}
}
The code above generates the following result.