C# WebClient UploadData(String, Byte[])

Description

WebClient UploadData(String, Byte[]) Uploads a data buffer to a resource identified by a URI.

Syntax

WebClient.UploadData(String, Byte[]) has the following syntax.


public byte[] UploadData(
  string address,
  byte[] data
)

Parameters

WebClient.UploadData(String, Byte[]) has the following parameters.

  • address - The URI of the resource to receive the data.
  • data - The data buffer to send to the resource.

Returns

WebClient.UploadData(String, Byte[]) method returns

Example

The following code example converts a string entered from the console to a Byte array and posts the array to the specified server using UploadData.


using System;/*www .  ja v a 2s .  co  m*/
using System.Net;
using System.Net.Sockets;
using System.Text;

public class Example
{
    public static void Main()
    {
        string uriString = "http://java2s.com";

        WebClient myWebClient = new WebClient();

        string postData = "data";

        byte[] postArray = Encoding.ASCII.GetBytes(postData);
        Console.WriteLine("Uploading to {0} ...", uriString);
        myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        byte[] responseArray = myWebClient.UploadData(uriString, postArray);
        Console.WriteLine("\nResponse received was :{0}", Encoding.ASCII.GetString(responseArray));


    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Net »




Cookie
Dns
IPAddress
IPEndPoint
IPHostEntry
WebClient