簡體   English   中英

具有相同鍵的C#WebRequest發布數組

[英]C# WebRequest Post Array with same key

我需要將數據發布到REST服務,並且它需要接受發布數據,例如

profile=high&profile=low

我目前正在這樣做:

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

我從該服務返回了409沖突,並查看它需要缺少第二個配置文件密鑰的日志。

還有另一種使用WebRequest發布數據的方法嗎?

干杯。

它不應該是writer.Write(data.Key + "=" + data.Value); 很難說,因為我們不知道您的KeyValuePair是什么樣。

我將字典更改為元組列表,以避免重復的鍵...這不是實際問題。 409是由於標題中的自定義Content-Type。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM