C#从NameValueCollection中发布值

时间:2011-11-17 20:35:56

标签: c# post webclient webclient.uploaddata

如何获取NameValueCollection值的POST的Content-Length?

我正在添加“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;
}

1 个答案:

答案 0 :(得分:0)

您无法在WebClient上设置该属性