C# WebClient UploadData(String, String, Byte[])

Description

WebClient UploadData(String, String, Byte[]) Uploads a data buffer to the specified resource, using the specified method.

Syntax

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


public byte[] UploadData(
  string address,//  w w w .  j  a  va 2 s.  c o  m
  string method,
  byte[] data
)

Parameters

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

  • address - The URI of the resource to receive the data.
  • method - The HTTP method used to send the data to the resource. If null, the default is POST for http and STOR for ftp.
  • data - The data buffer to send to the resource.

Returns

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

Example

The following code example converts a string entered into a byte array and posts the array to the specified server using UploadData.


using System;//from  w  ww  .  j  ava2 s .c  o m
using System.Net;
using System.Net.Sockets;
using System.Text;

public class Example
{
    public static void Main()
   {
            string uriString = "http://www.java2s.com";
      WebClient myWebClient = new WebClient();
      string postData = "data";
      myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
      Console.WriteLine(myWebClient.Headers.ToString());
      
      byte[] byteArray = Encoding.ASCII.GetBytes(postData);
      Console.WriteLine("Uploading to {0} ...",  uriString);            
      byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
      
          Encoding.ASCII.GetString(responseArray);

    
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Net »




Cookie
Dns
IPAddress
IPEndPoint
IPHostEntry
WebClient