public sealed class WebClient : Component
|
The WebClient class uses the WebRequest class to provide access to Internet resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest.RegisterPrefix method.
http:
,
https:
, and
file:
scheme identifiers.The WebClient class provides four methods for uploading data to a resource:
The WebClient class also provides three methods for downloading data from a resource:
ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. Initializes a new instance of the WebClient class. |
BaseAddress | Read-write Gets or sets the base URI for requests made by a WebClient. |
Container (inherited from System.ComponentModel.Component) |
Read-only See base class member description: System.ComponentModel.Component.Container Gets the IContainer that contains the Component. |
Credentials | Read-write Gets or sets the network credentials used to authenticate the request with the Internet resource. |
Headers | Read-write Gets or sets a collection of header name/value pairs associated with the request. |
QueryString | Read-write Gets or sets a collection of query name/value pairs associated with the request. |
ResponseHeaders | Read-only Gets a collection of header name/value pairs associated with the response. |
Site (inherited from System.ComponentModel.Component) |
Read-write See base class member description: System.ComponentModel.Component.Site Gets or sets the ISite of the Component. |
CreateObjRef (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
Dispose (inherited from System.ComponentModel.Component) |
Overloaded:Dispose() See base class member description: System.ComponentModel.Component.DisposeReleases all resources used by the Component. |
DownloadData | Downloads data from a resource with the specified URI. |
DownloadFile | Downloads data from a resource with the specified URI to a local file. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
InitializeLifetimeService (inherited from System.MarshalByRefObject) |
See base class member description: System.MarshalByRefObject.InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. |
OpenRead | Opens a readable stream for the data downloaded from a resource with the specified URI. |
OpenWrite | Overloaded:OpenWrite(string address) Opens a stream for writing data to the specified resource. |
OpenWrite | Overloaded:OpenWrite(string address, string method) Opens a stream for writing data to the specified resource with using the specified method. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
UploadData | Overloaded:UploadData(string address, byte[] data) Uploads a data buffer to a resource identified by a URI. |
UploadData | Overloaded:UploadData(string address, string method, byte[] data) Uploads a data buffer to the specified resource using the specified method. |
UploadFile | Overloaded:UploadFile(string address, string fileName) Uploads the specified local file to a resource with the specified URI. |
UploadFile | Overloaded:UploadFile(string address, string method, string fileName) Uploads the specified local file to the specified resource using the specified method. |
UploadValues | Overloaded:UploadValues(string address, NameValueCollection data) Uploads the specified name/value collection to the specified resource identified by a URI. |
UploadValues | Overloaded:UploadValues(string address, string method, NameValueCollection data) Uploads the specified name/value collection to the specified resource with the specified URI using the specified method. |
Disposed (inherited from System.ComponentModel.Component) |
See base class member description: System.ComponentModel.Component.Disposed Adds an event handler to listen to the Component.Disposed event on the component. |
DesignMode (inherited from System.ComponentModel.Component) |
Read-only See base class member description: System.ComponentModel.Component.DesignMode Gets a value that indicates whether the Component is currently in design mode. |
Events (inherited from System.ComponentModel.Component) |
Read-only See base class member description: System.ComponentModel.Component.Events Gets the list of event handlers that are attached to this Component. |
Dispose (inherited from System.ComponentModel.Component) |
Overloaded:Dispose(bool disposing) See base class member description: System.ComponentModel.Component.DisposeReleases the unmanaged resources used by the Component and optionally releases the managed resources. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
GetService (inherited from System.ComponentModel.Component) |
See base class member description: System.ComponentModel.Component.GetService Returns an object that represents a service provided by the Component or by its Container. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public WebClient(); |
try { // Download the data to a buffer. WebClient client = new WebClient(); Byte[] pageData = client.DownloadData("http://www.contoso.com"); string pageHtml = Encoding.ASCII.GetString(pageData); Console.WriteLine(pageHtml); // Download the data to a file. client.DownloadFile("http://www.contoso.com", "page.htm"); // Upload some form post values. NameValueCollection form = new NameValueCollection(); form.Add("MyName", "MyValue"); Byte[] responseData = client.UploadValues("http://www.contoso.com/form.aspx", form); } catch (WebException webEx) { Console.WriteLine(webEx.ToString()); if(webEx.Status == WebExceptionStatus.ConnectFailure) { Console.WriteLine("Are you behind a firewall? If so, go through the proxy server."); } }
public string BaseAddress {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | WebClient.BaseAddress is set to an invalid URI. |
If the WebClient.BaseAddress property is set, the URI specified when calling the following methods must be a relative URI:
hostUri
and that the path to the resource (such as /default.htm) is in
uriSuffix
.// Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Set the BaseAddress of the Web Resource in the WebClient. myWebClient.BaseAddress = hostUri; Console.WriteLine("Downloading from " + hostUri + "/" + uriSuffix); Console.WriteLine("\nPress Enter key to continue"); Console.ReadLine(); // Download the target Web Resource into a byte array. byte[] myDatabuffer = myWebClient.DownloadData (uriSuffix); // Display the downloaded data. string download = Encoding.ASCII.GetString(myDatabuffer); Console.WriteLine(download); Console.WriteLine("Download of " + myWebClient.BaseAddress.ToString() + uriSuffix + " was successful.");
public IContainer Container {get;}
|
public ICredentials Credentials {get; set;}
|
public static void Main() { try { WebClient client = new WebClient(); client.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = client.DownloadData("http://www.contoso.com"); string pageHtml = Encoding.ASCII.GetString(pageData); Console.WriteLine(pageHtml); } catch (WebException webEx) { Console.Write(webEx.ToString()); } }
protected bool DesignMode {get;}
|
protected EventHandlerList Events {get;}
|
public WebHeaderCollection Headers {get; set;}
|
string uriString; Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : "); uriString = Console.ReadLine(); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString); string postData = Console.ReadLine(); myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); // Apply ASCII Encoding to obtain the string as a byte array. byte[] byteArray = Encoding.ASCII.GetBytes(postData); Console.WriteLine("Uploading to {0} ...", uriString); // Upload the input string using the HTTP 1.0 POST method. byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray); // Decode and display the response. Console.WriteLine("\nResponse received was {0}", Encoding.ASCII.GetString(responseArray));
public NameValueCollection QueryString {get; set;}
|
string uriString = "http://www.contoso.com/search"; // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Create a new NameValueCollection instance to hold the QueryString parameters and values. NameValueCollection myQueryStringCollection = new NameValueCollection(); Console.Write("Enter the word(s), separated by space character to search for in " + uriString + ": "); // Read user input phrase to search for at uriString. string searchPhrase = Console.ReadLine(); if (searchPhrase.Length > 1) // Assign the user-defined search phrase. myQueryStringCollection.Add("q",searchPhrase); else // If error, default to search for 'Microsoft'. myQueryStringCollection.Add("q","Microsoft"); // Assign auxilliary parameters required for the search. Console.WriteLine("Searching " + uriString + " ......."); // Attach QueryString to the WebClient. myWebClient.QueryString = myQueryStringCollection; // Download the search results Web page into 'searchresult.htm' for inspection. myWebClient.DownloadFile (uriString, "searchresult.htm"); Console.WriteLine("\nDownload of " + uriString + " was successful. Please see 'searchresult.htm' for results.");
public WebHeaderCollection ResponseHeaders {get;}
|
// Obtain the WebHeaderCollection instance containing the header name/value pair from the response. WebHeaderCollection myWebHeaderCollection = myWebClient.ResponseHeaders; Console.WriteLine("\nDisplaying the response headers\n"); // Loop through the ResponseHeaders and display the header name/value pairs. for (int i=0; i < myWebHeaderCollection.Count; i++) Console.WriteLine ("\t" + myWebHeaderCollection.GetKey(i) + " = " + myWebHeaderCollection.Get(i));
public virtual ISite Site {get; set;}
|
The property value is null if the Component is removed from its IContainer. Assigning null to this property does not necessarily remove the Component from the IContainer.
A Component might or might not have a name. If a Component is given a name, the name must be unique among other Component objects within its IContainer. The ISite stores the name of the Component; therefore, you can only name a Component if it has an ISite associated with it.
requestedType
Exception Type | Condition |
---|---|
RemotingException | This instance is not a valid remoting object. |
public void Dispose(); |
protected virtual void Dispose( |
disposing
When the disposing parameter is true, this method releases all resources held by any managed objects that this Component references. This method invokes the Dispose() method of each referenced object.
For more information about Dispose and Object.Finalize, see the conceptual topic at MSDN: cleaningupunmanagedresources and the conceptual topic at MSDN: overridingfinalizemethod.
address
Exception Type | Condition |
---|---|
WebException | An error occurs while downloading data. |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
remoteUri
contains a valid URI for the requested data.// 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.");
address
fileName
Exception Type | Condition |
---|---|
WebException | An error occurs while downloading data. |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
string remoteUri = "http://www.contoso.com/library/homepage/images/"; string fileName = "ms-banner.gif", myStringWebResource = null; // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Concatenate the domain with the Web resource filename. myStringWebResource = remoteUri + fileName; Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource); // Download the Web resource and save it into the current filesystem folder. myWebClient.DownloadFile(myStringWebResource,fileName); Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource); Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath.);
~WebClient(); |
public virtual int GetHashCode(); |
public object GetLifetimeService(); |
service
This value is null if the Component does not provide the specified service.
public Type GetType(); |
public virtual object InitializeLifetimeService(); |
public class MyClass : MarshalByRefObject { public override Object InitializeLifetimeService() { ILease lease = (ILease)base.InitializeLifetimeService(); if (lease.CurrentState == LeaseState.Initial) { lease.InitialLeaseTime = TimeSpan.FromMinutes(1); lease.SponsorshipTimeout = TimeSpan.FromMinutes(2); lease.RenewOnCallTime = TimeSpan.FromSeconds(2); } return lease; } }
protected object MemberwiseClone(); |
address
Exception Type | Condition |
---|---|
WebException | An error occurs while downloading data. |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not null, it is appended to address.
uriString
and displays the results on the system console. Note that the Stream returned by WebClient.OpenRead is closed when the data has been read.// Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Download home page data. Console.WriteLine("Accessing {0} ...", uriString); // Open a stream to point to the data stream coming from the Web resource. Stream myStream = myWebClient.OpenRead(uriString); Console.WriteLine("\nDisplaying Data :\n"); StreamReader sr = new StreamReader(myStream); Console.WriteLine(sr.ReadToEnd()); // Close the stream. myStream.Close();
address
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
string uriString; Console.Write("\nPlease enter the URI to post data to : "); uriString = Console.ReadLine(); Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString); string postData = Console.ReadLine(); // Apply Ascii Encoding to obtain an array of bytes. byte[] postArray = Encoding.ASCII.GetBytes(postData); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // postStream implicitly sets HTTP POST as the request method. Console.WriteLine("Uploading to {0} ...", uriString); Stream postStream = myWebClient.OpenWrite(uriString); postStream.Write(postArray,0,postArray.Length); // Close the stream and release resources. postStream.Close(); Console.WriteLine("\nSuccessfully posted the data.");
address
method
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
If the method parameter specifies a method that is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the WebException.Status property set to indicate the error.
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
string uriString; Console.Write("\nPlease enter the URI to post data to : "); uriString = Console.ReadLine(); Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString); string postData = Console.ReadLine(); // Apply ASCII encoding to obtain an array of bytes . byte[] postArray = Encoding.ASCII.GetBytes(postData); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); Console.WriteLine("Uploading to {0} ...", uriString); Stream postStream = myWebClient.OpenWrite(uriString,"POST"); postStream.Write(postArray,0,postArray.Length); // Close the stream and release resources. postStream.Close(); Console.WriteLine("\nSuccessfully posted the data.");
public virtual string ToString(); |
address
data
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
The POST verb is defined by HTTP. If the underlying request does not use HTTP and POST is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the WebException.Status property set to indicate the error.
The WebClient.UploadData method sends the content of data to the server without encoding it.
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
Console.Write("\nPlease enter the URI to post data to : "); string uriString = Console.ReadLine(); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString); string postData = Console.ReadLine(); // Apply ASCII Encoding to obtain the string as a byte array. byte[] postArray = Encoding.ASCII.GetBytes(postData); Console.WriteLine("Uploading to {0} ...", uriString); myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); //UploadData implicitly sets HTTP POST as the request method. byte[] responseArray = myWebClient.UploadData(uriString,postArray); // Decode and display the response. Console.WriteLine("\nResponse received was :{0}", Encoding.ASCII.GetString(responseArray));
address
method
data
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
The WebClient.UploadData method sends the content of data to the server without encoding it.
If the method parameter specifies a verb that is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the WebException.Status property set to indicate the error.
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
string uriString; Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : "); uriString = Console.ReadLine(); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString); string postData = Console.ReadLine(); myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded"); // Apply ASCII Encoding to obtain the string as a byte array. byte[] byteArray = Encoding.ASCII.GetBytes(postData); Console.WriteLine("Uploading to {0} ...", uriString); // Upload the input string using the HTTP 1.0 POST method. byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray); // Decode and display the response. Console.WriteLine("\nResponse received was {0}", Encoding.ASCII.GetString(responseArray));
address
fileName
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. -or- The Content-type header begins with "multipart". |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
The POST verb is defined by HTTP. If the underlying request does not use HTTP and POST is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the WebException.Status property set to indicate the error.
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
Console.Write("\nPlease enter the URI to post data to : "); String uriString = Console.ReadLine(); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI"); string fileName = Console.ReadLine(); Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString); // Upload the file to the URI. // The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method. byte[] responseArray = myWebClient.UploadFile(uriString,fileName); // Decode and display the response. Console.WriteLine("\nResponse Received.The contents of the file uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));
address
method
fileName
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. -or- The Content-type header begins with "multipart". |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
If the method parameter specifies a verb that is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the WebException.Status property set to indicate the error.
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
Console.Write("\nPlease enter the URL to post data to : "); String uriString = Console.ReadLine(); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URL"); string fileName = Console.ReadLine(); Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString); // Upload the file to the URL using the HTTP 1.0 POST. byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName); // Decode and display the response. Console.WriteLine("\nResponse Received.The contents of the file uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));
public byte[] UploadValues( |
address
data
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. -or- The Content-type header is not "application/x-www-form-urlencoded". |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
The POST verb is defined by HTTP. If the underlying request does not use HTTP and POST is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the WebException.Status property set to indicate the error.
The WebClient.UploadValues method always sets the Content-type header to "Content-type: application/x-www-form-urlencoded".
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
Console.Write("\nPlease enter the URI to post data to : "); string uriString = Console.ReadLine(); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL. NameValueCollection myNameValueCollection = new NameValueCollection(); Console.WriteLine("Please enter the following parameters to be posted to the URL"); Console.Write("Name:"); string name = Console.ReadLine(); Console.Write("Age:"); string age = Console.ReadLine(); Console.Write("Address:"); string address = Console.ReadLine(); // Add necessary parameter/value pairs to the name/value container. myNameValueCollection.Add("Name",name); myNameValueCollection.Add("Address",address); myNameValueCollection.Add("Age",age); Console.WriteLine("\nUploading to {0} ...", uriString); // 'The Upload(String,NameValueCollection)' implicitly method sets HTTP POST as the request method. byte[] responseArray = myWebClient.UploadValues(uriString,myNameValueCollection); // Decode and display the response. Console.WriteLine("\nResponse received was :\n{0}",Encoding.ASCII.GetString(responseArray));
public byte[] UploadValues( |
address
method
data
Exception Type | Condition |
---|---|
WebException | An error occurs while opening the stream. -or- The Content-type header is not "application/x-www-form-urlencoded". |
UriFormatException | The URI formed by combining WebClient.BaseAddress, address, and WebClient.QueryString is invalid. |
The WebClient.UploadValues method always sets the Content-type header to "Content-type: application/x-www-form-urlencoded".
If the method parameter specifies a verb that is not understood by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the WebException.Status property set to indicate the error.
If the WebClient.BaseAddress property is not empty, address must be a relative URI that is combined with WebClient.BaseAddress to form the absolute URI of the requested data. If the WebClient.QueryString property is not empty, it is appended to address.
Console.Write("\nPlease enter the URL to post data to : "); string uriString = Console.ReadLine(); // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL. NameValueCollection myNameValueCollection = new NameValueCollection(); Console.WriteLine("Please enter the following parameters to be posted to the URI"); Console.Write("Name:"); string name = Console.ReadLine(); Console.Write("Age:"); string age = Console.ReadLine(); Console.Write("Address:"); string address = Console.ReadLine(); // Add necessary parameter/value pairs to the name/value container. myNameValueCollection.Add("Name",name); myNameValueCollection.Add("Address",address); myNameValueCollection.Add("Age",age); Console.WriteLine("\nUploading to {0} ...", uriString); // Upload the NameValueCollection. byte[] responseArray = myWebClient.UploadValues(uriString,"POST",myNameValueCollection); // Decode and display the response. Console.WriteLine("\nResponse received was :\n{0}",Encoding.ASCII.GetString(responseArray));
public event EventHandler Disposed;
|