C# 从 NameValueCollection 发送值

发布于 2024-12-16 00:31:19 字数 608 浏览 2 评论 0原文

如何获取 NameValueCollection 值的 POST 的内容长度?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日暮斜阳 2024-12-23 00:31:19

您无法在 WebClient 上设置该属性

You can not set that property on a WebClient

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文