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

SolidSnake picture SolidSnake · Mar 23, 2011 · Viewed 482.1k times · Source

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 wanna use WebClient instead. Is that possible? If so, can someone show me some example or point me to the right direction?

Answer

SolidSnake picture SolidSnake · Mar 23, 2011

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 :)