Ian Vink
Ian Vink

Reputation: 68830

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;
}

Upvotes: 0

Views: 1701

Answers (1)

Ian Vink
Ian Vink

Reputation: 68830

You can not set that property on a WebClient

Upvotes: 0

Related Questions