Search code examples
c#postwebclientwebclient.uploaddata

C# POSTing values from NameValueCollection


How do I get the Content-Length for a POST of NameValueCollection values?

I am adding the "Content-Length" header, but don't know what to put in it.

public static string Post (string url, NameValueCollection formData)
{
    string response;
    int length = formData.???

    using (WebClient webClient = new WebClient())
    {
        webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        webClient.Headers.Add ("Content-Length", length);
        byte[] responseBytes = webClient.UploadValues (url, "POST", formData);
        response = Encoding.UTF8.GetString (responseBytes);
    }

    return response;
}

Solution

  • You can not set that property on a WebClient