public sealed class WebClient : MarshalByRefObject
Object
MarshalByRefObject
WebClientThis type implements IDisposable.
System
Networking
Provides common methods for sending data to and receiving data from a resource identified by a URI.
[Note: The WebClient class provides common methods for sending data to or receiving data from any local, Intranet, or Internet resource identified by a URI.The WebClient class uses the WebRequest class to provide access to Internet resources. WebClient instances can access data with any class derived from WebRequest that is registered with the System.Net.WebRequest.RegisterPrefix(System.String,System.Net.IWebRequestCreate) method.
By default, the CLI supports URIs with the "http:", "https:", and "file:" schemes.
The WebClient class provides the following methods for uploading data to a resource.
The WebClient class also provides the following methods for downloading data from a resource.
- System.Net.WebClient.OpenWrite(System.String) sends a Stream to the server hosting a resource.
- System.Net.WebClient.UploadData(System.String,System.Byte[]) sends a byte array to the server hosting a resource and returns a byte array containing the response from the server, if any.
- System.Net.WebClient.UploadFile(System.String,System.String) sends a local file to the server hosting a resource and returns a byte array containing the response from the server, if any.
- System.Net.WebClient.UploadValues(System.String,System.Collections.Specialized.NameValueCollection) sends a NameValueCollection collection to the server hosting a resource and returns a byte array containing the response from the server, if any.
]
- System.Net.WebClient.DownloadData(System.String) downloads data from a resource and returns a byte array.
- System.Net.WebClient.DownloadFile(System.String,System.String) downloads data from a resource to a local file.
- System.Net.WebClient.OpenRead(System.String) returns the data from the resource as a Stream.
System.Net Namespace
WebClient Constructors
WebClient Methods
WebClient.DownloadData Method
WebClient.DownloadFile Method
WebClient.OpenRead Method
WebClient.OpenWrite(System.String) Method
WebClient.OpenWrite(System.String, System.String) Method
WebClient.UploadData(System.String, byte[]) Method
WebClient.UploadData(System.String, System.String, byte[]) Method
WebClient.UploadFile(System.String, System.String) Method
WebClient.UploadFile(System.String, System.String, System.String) Method
WebClient.UploadValues(System.String, System.Collections.Specialized.NameValueCollection) Method
WebClient.UploadValues(System.String, System.String, System.Collections.Specialized.NameValueCollection) Method
WebClient Properties
WebClient.BaseAddress Property
WebClient.Credentials Property
WebClient.Headers Property
WebClient.QueryString Property
WebClient.ResponseHeaders Property
public WebClient();
Constructs a new instance of the WebClient class.
This constructor creates a new instance of the WebClient class with properties set tonull
.
System.Net.WebClient Class, System.Net Namespace
public byte[] DownloadData(string address);
Downloads data from the resource identified by the specified URI.
- address
- A String that specifies the URI from which data will be downloaded.
A Byte array containing the data downloaded from the resource specified by address.
Exception Type Condition WebException The absolute URI is not valid. -or-
An error occurred while downloading data.
If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.
System.Net.WebClient Class, System.Net Namespace
public void DownloadFile(string address, string fileName);
Downloads data from the resource identified by the specified URI, and writes the data to the specified file .
- address
- A String that specifies the URI from which data will be downloaded.
- fileName
- A String that specifies the name of the local file to which data will be written.
Exception Type Condition WebException The absolute URI is not valid. -or-
filename is
null
or System.String.Empty or contains invalid characters, or the specified path to the file does not exist.-or-
An error occurred while downloading data.
SecurityException The caller does not have permission to write to local files.
If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.This method downloads data from the absolute URI to local file, fileName . If fileName already exists, the existing file is overwritten.
System.Net.WebClient Class, System.Net Namespace
public Stream OpenRead(string address);
Opens a readable Stream containing the data downloaded from the resource identified by the specified URI.
- address
- A String that specifies the URI from which data will be downloaded.
A Stream used to read data from a resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
An error occurred while downloading data.
If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.[Note: This method creates a Stream instance used to access the data specified by the absolute URI . The caller of this method is responsible for calling System.IO.Stream.Close to release the resources allocated for the stream.]
System.Net.WebClient Class, System.Net Namespace
public Stream OpenWrite(string address);
Opens a Stream for writing data to the resource identified by the specified URI.
- address
- A String that specifies the URI to receive the data.
A Stream used to write data to the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
An error occurred while opening the stream.
This method is equivalent to System.Net.WebClient.OpenWrite(System.String)(address, "POST").If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.
[Note: The underlying request is made with the POST method.]
System.Net.WebClient Class, System.Net Namespace
public Stream OpenWrite(string address, string method);
Opens a Stream for writing data to the resource identified by the specified URI using the specified protocol method.
- address
- A String that specifies the URI of the resource to receive the data.
- method
- A String that specifies the protocol method used to send the data to the resource identified by address .
A Stream used to write data to the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
An error occurred while opening the stream.
If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.[Note: This method returns a writable stream that is used to send data to a resource. The underlying request is made with the protocol method specified by method . For more information about protocol methods, see System.Net.WebRequest.Method .
If the method parameter specifies a method that is not recognized by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the System.Net.WebException.Status property set to indicate the error.
]
System.Net.WebClient Class, System.Net Namespace
public byte[] UploadData(string address, byte[] data);
Uploads the specified data to the resource identified by the specified URI.
- address
- A String that specifies the URI of the resource to receive the data.
- data
- A Byte array containing data to send to the resource.
A Byte array containing the body of the response, if any, from the server hosting the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
data is
null
.-or-
An error occurred while opening the stream or uploading the data.
-or-
There was no response from the server hosting the resource.
This method is equivalent to System.Net.WebClient.UploadData(System.String,System.Byte[]) (address, "POST", data).If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.
[Note: This method sends a data buffer to a resource. The underlying request is made using the POST method.
This method does not encode the contents of data before uploading it to the resource.
]
System.Net.WebClient Class, System.Net Namespace
public byte[] UploadData(string address, string method, byte[] data);
Uploads the specified data to the resource identified by the specified URI using the specified protocol method.
- address
- A String that specifies the URI of the resource to receive the data.
- method
- A String that specifies the protocol method used to send the data to the resource identified by address .
- data
- A Byte array containing data to send to the resource.
A Byte array containing the body of the response, if any, from the server hosting the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
data is
null
.-or-
An error occurred while opening the stream or uploading the data.
-or-
There was no response from the server hosting the resource.
If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.[Note: For more information about protocol methods, see System.Net.WebRequest.Method .
This method does not encode the contents of data before uploading it to the resource.
If method specifies a method that is not recognized by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the System.Net.WebException.Status property set to indicate the error.
]
System.Net.WebClient Class, System.Net Namespace
public byte[] UploadFile(string address, string fileName);
Uploads the specified local file to the resource identified by the specified URI.
- address
- A String that specifies the URI of the resource to receive the file.
- fileName
- A String that specifies the file to send to the resource.
A Byte array containing the body of the response, if any, from the server hosting the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
filename is
null
or System.String.Empty or contains invalid characters, or the specified path to the file does not exist.-or-
An error occurred while opening the stream or uploading the file.
-or-
There was no response from the server hosting the resource.
-or-
The Content-Type header begins with "multipart".
SecurityException Local file access has not been granted.
This method is equivalent to System.Net.WebClient.UploadFile(System.String,System.String) (address, "POST", filename).If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.
[Note: This method sends a local file to a resource. The underlying request is made using the POST method.]
System.Net.WebClient Class, System.Net Namespace
public byte[] UploadFile(string address, string method, string fileName);
Uploads the specified local file using the specified protocol method to the resource identified by the specified URI .
- address
- A String that specifies the URI of the resource to receive the file.
- method
- A String that specifies the protocol method used to send the data to the resource identified by address .
- fileName
- A String that specifies the file to send to the resource.
A Byte array containing the body of the response, if any, from the server hosting the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
filename is
null
or System.String.Empty or contains invalid characters, or the specified path to the file does not exist.-or-
An error occurred while opening the stream or uploading the file.
-or-
There was no response from the server hosting the resource.
-or-
The Content-Type header begins with "multipart".
SecurityException The caller does not have the required permissions.
If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.[Note: For more information about protocol methods, see System.Net.WebRequest.Method .
If method specifies a protocol method that is not recognized by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the System.Net.WebException.Status property set to indicate the error.
]
System.Net.WebClient Class, System.Net Namespace
public byte[] UploadValues(string address, NameValueCollection data);
Uploads the specified name/value collection to the specified resource.
- address
- A String that specifies the URI of the resource to receive the collection data .
- data
- The NameValueCollection to send to the resource identified by address .
A Byte array containing the body of the response, if any, from the server hosting the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
data is
null
.-or-
An error occurred while opening the stream or uploading the data.
-or-
There was no response from the server hosting the resource.
-or-
The Content-Type header is not
null
, and is not "application/x-www-form-urlencoded".
This method is equivalent to System.Net.WebClient.UploadValues(System.String,System.Collections.Specialized.NameValueCollection) (address, "POST", data).If the Content-Type header is
null
, this method sets it to "application/x-www-form-urlencoded".If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.
[Note: The underlying request is made using the POST method. ]
System.Net.WebClient Class, System.Net Namespace
public byte[] UploadValues(string address, string method, NameValueCollection data);
Uploads the specified name/value collection to the specified resource using the specified method.
- address
- A String that specifies the URI of the resource to receive the collection data .
- method
- A String that specifies the protocol method used to send data to the resource.
- data
- The NameValueCollection to send to the resource identified by address .
A Byte array containing the body of the response, if any, from the server hosting the resource.
Exception Type Condition WebException The absolute URI is not valid. -or-
data is
null
.-or-
An error occurred while opening the stream or uploading the data.
-or-
There was no response from the server hosting the resource.
-or-
The Content-Type header is not
null
, and is not "application/x-www-form-urlencoded".
If the Content-Type header isnull
, this method sets it to "application/x-www-form-urlencoded".If the System.Net.WebClient.BaseAddress property of the current instance is not System.String.Empty, address is combined with System.Net.WebClient.BaseAddress to form the absolute URI of the requested data. If the System.Net.WebClient.BaseAddress property of the current instance is System.String.Empty, address is required to be the absolute URI of the requested data. If the System.Net.WebClient.QueryString property of the current instance is not System.String.Empty, it is appended to address.
[Note: If method specifies a protocol method that is not recognized by the server, the underlying protocol classes determine what occurs. Typically, a WebException is thrown with the System.Net.WebException.Status property set to indicate the error.]
System.Net.WebClient Class, System.Net Namespace
public string BaseAddress { get; set; }
Gets or sets the base URI for requests made by a WebClient .
A String containing the base URI for requests made by a WebClient or, System.String.Empty, if no value was set ornull
was specified for a set operation.
Exception Type Condition ArgumentException The value specified for a set operation is not null
or System.String.Empty, and is not a URI in a format recognized by the Uri class.
[Note: The System.Net.WebClient.BaseAddress property contains a base URI that is combined with the relative address specified when calling an upload or download method.]
System.Net.WebClient Class, System.Net Namespace
public ICredentials Credentials { get; set; }
Gets or sets the network credentials used to authenticate the client making the current request.
A ICredentials containing the authentication credentials for the request. The default isnull
.
The System.Net.WebClient.Credentials property contains the authentication credentials required to access the Internet resource.
System.Net.WebClient Class, System.Net Namespace
public WebHeaderCollection Headers { get; set; }
Gets or sets a collection of header name/value pairs associated with the request.
A WebHeaderCollection containing header name/value pairs associated with the request or, if this property has not been set or was set tonull
, a new instance of the WebHeaderCollection class.
[Note: This property stores the header information that the current instance sends with a request. This is an unrestricted collection of headers; setting headers that are protected in WebRequest descendants like HttpWebRequest is allowed.]
System.Net.WebClient Class, System.Net Namespace
public NameValueCollection QueryString { get; set; }
Gets or sets a collection of query name/value pairs associated with the request.
A NameValueCollection that contains query name/value pairs associated with the request or, if this property has not been set or was set tonull
, a new instance of the NameValueCollection class.
The System.Net.WebClient.QueryString property contains a NameValueCollection instance containing name/value pairs that are appended to the URI as a query string. The contents of the System.Net.WebClient.QueryString property are preceded by a question mark (?), and each name/value pair is separated by an ampersand (&).
System.Net.WebClient Class, System.Net Namespace
public WebHeaderCollection ResponseHeaders { get; }
Gets a collection of header name/value pairs associated with the response.
A WebHeaderCollection containing header name/value pairs associated with the response.
This property is read-only.The System.Net.WebClient.ResponseHeaders property contains a WebHeaderCollection instance containing header information the WebClient receives from the Internet resource.
System.Net.WebClient Class, System.Net Namespace