How to upload values to the server for Processing using Webclient class

I want to discuss an article to upload values to other server which is running in not only in .net platform Other than .Net for an ex. PHP e.t.c. How to upload these values in .Net using webclient class and sent to server for Processing ?

Sometimes we come across a scenario that we need to sync the data in central server database and as well as in Local Database. For that in .Net there is a easiest way to upload the values and sent to the server one way of doing it is through web service but the easiest way of doing it is in .NET is through WebClient.

In .Net WebClient Class will come after importing the Namespace or Package i.e. System.Net

In c# it is Using system.Net

In it is imports system.Net

In WebClient class there are several Methods Associated in it.They are


The UploadData() Method will take parameters/Arguments string address ,string Method and byte data.

WebClient.UploadData(string Address,string Method,byte[]data);

The UploadValues()will take Parameters string Address,string Method, System.Collection.Specialized.NameValueCollection

WebClient.UploadValues(string Address,string Method,System.Collection.Specialized.NameValueCollection)

WebClient.UploadValues(string Address,System.Collection.Specialized.NameValueCollection)

NameValueCollection will come under the Namespace/Package in System.Collections.Specialized.

It is like almost an Hashtable. But unlike Hashtable the add method will have string and string arguments

System.Collection.Specialized.NameValueCollection nam1 = System.Collection.Specialized.NameValueCollection();

nam1.add(string name ,string value);

now let us see the Screen shots how to use NamevalueCollection.


How do we pass the URL parameter/argument in the Webclient class below is the code snippet for that.

string URI = @"http://XXX.XXX.XXX.XX/XXXXXX/index.php?fn=addSession";
WebClient wc = new WebClient();

Webc URL

There are two important things we need to remember here

Note 1 :

You can pass as one of the argument in this Method get/post which is Optional(WebClient.UploadValues(string Address,string Method,System.Collection.Specialized.NameValueCollection)).

for ex: WebClient.uploadValues("ipaddress/Domainame","get",NamedvalueCollection)

Note 2: You need to specify the Header Content type which is optional only if you specify URL (form URL Encoded) like in this scenario,if you specify other than this(URL/URI type) you need to Mention the Header type otherwise the server will not parse your data and it will fail when values are upload to the server.

Now the important thing we need to know whether the Uploaded values are successfully sent to the server for processing or not for that we need to see the below screen shot at run time.

New Result.

After the data is processed in server with the Uploaded values from the Client it will return the result in Byte value i had Converted that byte value to string value using AsciiEncoding.Ascii.GetString(byte value) Method where value 1 denotes the server has Successful uploaded and processed the values , the value 0 or null denotes failure or even if you get an exception

Full Code :

string URI = @"";
WebClient wc = new WebClient();
NameValueCollection inputs = new NameValueCollection();
inputs.Add("fn", "addSession");
inputs.Add("id", sessionid);
inputs.Add("step", stepid.ToString());
inputs.Add("option", type);
inputs.Add("date", datetimenow.ToString());
inputs.Add("view", "0");
inputs.Add("datefrom", fromdate.ToString());
inputs.Add("dateto", todate);
inputs.Add("numfrom", numfrom.ToString());
inputs.Add("numto", numto.ToString());
inputs.Add("word", EncryptionDecryption.Encrypt(keywords.ToString(), true));
inputs.Add("phrase", EncryptionDecryption.Encrypt(phrases.ToString(), true));
inputs.Add("other", otherfiledetails);

byte[] retvalue = wc.UploadValues(URI, inputs);
string value = ASCIIEncoding.ASCII.GetString(retvalue);


  • Webclient (45805-203715-Webclient.bmp)
  • Article by srirama
    A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

    Follow srirama or read 73 articles authored by srirama


    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: