Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post data to specific URL using WebClient in C#

Tags:

c#

post

webclient

I need to use "HTTP Post" with WebClient to post some data to a specific URL I have.

Now, I know this can be accomplished with WebRequest but for some reasons I want to use WebClient instead. Is that possible? If so, can someone show me some example or point me to the right direction?

like image 882
Desolator Avatar asked Mar 23 '11 06:03

Desolator


People also ask

How do you post data on WebClient?

The WebClient class uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered with the WebRequest. RegisterPrefix method. UploadString Sends a String to the resource and returns a String containing any response.

How do you send parameters data using WebClient POST request in C?

var url = "https://your-url.tld/expecting-a-post.aspx" var client = new WebClient(); var method = "POST"; // If your endpoint expects a GET then do it. var parameters = new NameValueCollection(); parameters. Add("parameter1", "Hello world"); parameters. Add("parameter2", "www.stopbyte.com"); parameters.

Is there an alternative to WebClient?

NET 4.5 platform the community developed an alternative. Today, RestSharp is one of the only options for a portable, multi-platform, unencumbered, fully open-source HTTP client that you can use in all of your applications. It combines the control of HttpWebRequest with the simplicity of WebClient .

Why do we use WebClient in C#?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.


1 Answers

I just found the solution and yea it was easier than I thought :)

so here is the solution:

string URI = "http://www.myurl.com/post.php"; string myParameters = "param1=value1&param2=value2&param3=value3";  using (WebClient wc = new WebClient()) {     wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";     string HtmlResult = wc.UploadString(URI, myParameters); } 

it works like charm :)

like image 55
Desolator Avatar answered Oct 02 '22 20:10

Desolator