简体   繁体   English

具有相同键的C#WebRequest发布数组

[英]C# WebRequest Post Array with same key

I need to post data to a REST service and it needs to accept post data such as 我需要将数据发布到REST服务,并且它需要接受发布数据,例如

profile=high&profile=low

I am currently doing this: 我目前正在这样做:

Dictionary<string, string> postData = new Dictionary<string, string>();
postData.Add("source=", streamSource);
postData.Add("ipvs=", "false");
postData.Add("stream=", string.Empty);
postData.Add("output=", "high");
postData.Add("framerateDivider=", "1");
postData.Add("resolution=", "1");
postData.Add("profile=", "high");
postData.Add("&output=", "low");
postData.Add("&framerateDivider=", "1");
postData.Add("&resolution=", "1");
postData.Add("ipvsProfile=", "high");
postData.Add("ipvsTitle=", string.Empty);
postData.Add("ipvsDescription=", string.Empty);
postData.Add("ipvsTagName=", string.Empty);
postData.Add("ipvsTagValue=", string.Empty);

using (Stream stream = request.GetRequestStream())
{
    using (var writer = new StreamWriter(stream))
    {
        foreach (KeyValuePair<string, string> data in postData)
        {
            if (data.Key.StartsWith("&"))
            {
                writer.Write(data.Key.Substring(1), data.Value);
            }
            else
            {
                writer.Write(data.Key + data.Value);
            }
        }

        writer.Close();
    }

    stream.Close();
}

I get a 409 conflict returned from the service and looking at it's logs it needs it is missing the second profile key. 我从该服务返回了409冲突,并查看它需要缺少第二个配置文件密钥的日志。

Is there another way to Post data using WebRequest? 还有另一种使用WebRequest发布数据的方法吗?

Cheers. 干杯。

Shouldn't it be writer.Write(data.Key + "=" + data.Value); 它不应该是writer.Write(data.Key + "=" + data.Value); It's hard to say because we don't know what your KeyValuePair looks like. 很难说,因为我们不知道您的KeyValuePair是什么样。

I changed the dictionary to a list of Tuple to avoid duplicate key's... which is not the actual problem. 我将字典更改为元组列表,以避免重复的键...这不是实际问题。 The 409 was because of a custom Content-Type in the headers. 409是由于标题中的自定义Content-Type。

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

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