C# WebClient

C# WebClient

C# WebClient is used for posting or crawling the data from server to server. It is very similar to HttpWebRequest. Using Web Client in C# you can post the data to web with Name Value collection.

Note*: you can't set the timeout property for WebClient instance.

using System.Net; 
using System.Collections.Specialized;


WebClient wc = new WebClient();

wc.BaseAddress = "Http://yourDomain.com";
 
NameValueCollection nc = new NameValueCollection(); 

nc.Add("FormField1","your_value_to_post1"); 

nc.Add("FormField2","your_value_to_post2"); 

Byte []arr = wc.UploadValues(wc.BaseAddress,nc);   

// you can overload uploadValues function to add method name like "POST" or "GET" here . 

string strResponse = System.Text.Encoding.ASCII.GetString(arr); 
  
Description

With the help of NameValueCollection you can post the parameters to third party web. Add those require parameters to post in instance of name value collection and get the result in byte array. Convert the byte stream into String representation and store the data if required.

Tags:

C# foreach loopc# arraylistc# stringC# DateTime DifferenceC# XML XmlReaderC# Binary To StringC# Regular Expression Get Text Between BracketsC# For LoopReplace string in C#C# Break StatementC# while loop

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.