This documentation is archived and is not being maintained.

WebClient.UploadValues Method (String, NameValueCollection)

.NET Framework 1.1

Uploads the specified name/value collection to the specified resource identified by a URI.

[Visual Basic]
Overloads Public Function UploadValues( _
   ByVal address As String, _
   ByVal data As NameValueCollection _
) As Byte()
[C#]
public byte[] UploadValues(
 string address,
 NameValueCollection data
);
[C++]
public: unsigned char UploadValues(
 String* address,
 NameValueCollection* data
)  __gc[];
[JScript]
public function UploadValues(
   address : String,
 data : NameValueCollection
) : Byte[];

Parameters

address
The URI of the resource to receive the collection.
data
The NameValueCollection to send to the resource.

Return Value

An array of bytes containing the body of any response from the resource.

Exceptions

Exception Type Condition
WebException The URI formed by combining BaseAddress, and address is invalid.

-or-

data is a null reference (Nothing in Visual Basic).

-or-

There was no response from the server hosting the resource.

-or-

An error occured while opening the stream.

-or-

The Content-type header is not a null reference (Nothing) or "application/x-www-form-urlencoded".

Remarks

The UploadValues method sends a NameValueCollection to an Internet server. The underlying request is made using the POST method verb.

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 Status property set to indicate the error.

If the Content-type header is a null reference (Nothing in Visual Basic), the UploadValues method sets it to "application/x-www-form-urlencoded".

If the BaseAddress property is not empty, address must be a relative URI that is combined with BaseAddress to form the absolute URI of the requested data. If the QueryString property is not empty, it is appended to address.

Example

[Visual Basic, C#, C++] The following example gathers information from the user (name, age, and address) and posts the values to the server using UploadValues. Any response from the server is displayed on the console.

[Visual Basic] 
Console.Write(ControlChars.Cr + "Please enter the URI to post data to : ")
Dim uriString As String = Console.ReadLine()
' Create a new WebClient instance.
Dim myWebClient As New WebClient()
' Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL.
Dim myNameValueCollection As New NameValueCollection()
Console.WriteLine("Please enter the following parameters to be posted to the URL:")
Console.Write("Name:")
Dim name As String = Console.ReadLine()

Console.Write("Age:")
Dim age As String = Console.ReadLine()

Console.Write("Address:")
Dim address As String = 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(ControlChars.Cr + "Uploading to {0} ...", uriString)
' The Upload(String,NameValueCollection)' method implicitly sets the HTTP POST as the request method.            
Dim responseArray As Byte() = myWebClient.UploadValues(uriString, myNameValueCollection)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :" + ControlChars.Cr + "{0}", Encoding.ASCII.GetString(responseArray))

[C#] 
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));

[C++] 
Console::Write(S"\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(S"Please enter the following parameters to be posted to the URL");
Console::Write(S"Name:");
String* name = Console::ReadLine();

Console::Write(S"Age:");
String* age = Console::ReadLine();

Console::Write(S"Address:");
String* address = Console::ReadLine();

// Add necessary parameter/value pairs to the name/value container.
myNameValueCollection->Add(S"Name", name);
myNameValueCollection->Add(S"Address", address);
myNameValueCollection->Add(S"Age", age);

Console::WriteLine(S"\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(S"\nResponse received was :\n {0}", Encoding::ASCII->GetString(responseArray));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

WebClient Class | WebClient Members | System.Net Namespace | WebClient.UploadValues Overload List

Show: