Export (0) Print
Expand All

WebClient.UploadData Method (String, Byte[])

.NET Framework 1.1

Uploads a data buffer to a resource identified by a URI.

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

Parameters

address
The URI of the resource to receive the data.
data
The data buffer 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-

An error occurred while opening the stream.

-or-

There was no response from the server hosting the resource.

Remarks

The UploadData method sends a data buffer to a resource. 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.

The UploadData method sends the content of data to the server without encoding it.

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 converts a string entered from the console to a byte array and posts the array to the specified server using UploadData. Any response from the server is displayed to 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()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
' Apply ASCII Encoding to obtain the string as a byte array.
Dim postArray As Byte() = 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.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, postArray)

' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{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();
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));

[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();
Console::WriteLine(S"\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(S"Uploading to {0} ...", uriString);
myWebClient->Headers->Add(S"Content-Type", S"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(S"\nResponse received was : {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.UploadData Overload List

Show:
© 2014 Microsoft