简体   繁体   English

C#从NameValueCollection中发布值

[英]C# POSTing values from NameValueCollection

How do I get the Content-Length for a POST of NameValueCollection values? 如何获得NameValueCollection值的POST的Content-Length?

I am adding the "Content-Length" header, but don't know what to put in it. 我正在添加“ Content-Length”标头,但不知道要放什么。

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

您不能在WebClient上设置该属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM