Search code examples
c#webclient

POST Querystring using WebClient


I want to execute a QueryString using WebClient but using the POST Method

That is what I got so far

CODE:

using (var client = new WebClient())
{
    client.QueryString.Add("somedata", "value");
    client.DownloadString("uri");
}

It is working but unfortunately it is using GET not POST and the reason I want it to use POST is that I am doing a web scraping and this is how the request is made as I see in WireShark. [IT USES POST AS A METHOD BUT NO POST DATA ONLY THE QUERY STRING.]


Solution

  • In answer to your specific question:

    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    byte[] response = client.UploadData("your url", "POST", new byte[] { });
    //get the response as a string and do something with it...
    string s = System.Text.Encoding.Default.GetString(response);
    

    But using WebClient can be a PITA since it doesn't accept cookies nor allow you to set a timeout.