如何使用重复键构建WebClient查询字符串? [英] How to build WebClient querystring with duplicate keys?

查看:124
本文介绍了如何使用重复键构建WebClient查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据发布到服务,该服务要求我提交重复的查询字符串键(丑陋且未在任何标准中指定).

I'm posting data to a service that requires that I submit duplicate query string keys (ugly and not specified in any standards).

我正在使用WebClient对象构建请求.我想继续使用它,因为它在项目中的其他地方经常使用.

I'm using WebClient object to build the request. I'd like to keep using it since it is used frequently elsewhere in the project.

当我这样做

 foreach(var f in formats)                
      client.QueryString.Add("formats", f);

我得到服务不支持的列表&formats=format_1,format_2,format_3.

I get a list &formats=format_1,format_2,format_3 which the service does not support.

有没有比这种过时的丑陋更好的选择:

Is there a better alternative than this old-school ugliness:

var extraQueryString = string.Empty;

extraQueryString += "?apiKey=" + TRANSCODE_KEY;
extraQueryString += "&fileKey=" + fileKey;

foreach (var f in formats)     
      extraQueryString += "&formats=" + f;

var response = client.UploadData(TRANSCODE_URI + "task" + extraQueryString , new byte[] { });

推荐答案

之所以这样,是因为NameValueCollection用逗号分隔重复的键.您可以扩展NameValueCollection并覆盖Get方法,并使它返回所需的格式.

The reason for this is because the NameValueCollection separates duplicate keys with commas. You could extend the NameValueCollection and override the Get method and have it return the format you want.

public class DupeNVC: NameValueCollection
{
    private string _duplicateKey;
    public DupeNVC(string duplicateKey = null)
    {
        _duplicateKey = duplicateKey;
    }

    public override string Get(int index)
    {
        //check if duplicate key has been specified
        //if not, then call the default Get implementation
        if (!String.IsNullOrEmpty(_duplicateKey))
        {
            ArrayList list = (ArrayList)base.BaseGet(index);              

            int num = (list != null) ? list.Count : 0;
            if (num == 1)
            {
                return (string)list[0];
            }
            if (num > 1)
            {
                StringBuilder stringBuilder = new StringBuilder((string)list[0]);

                for (int i = 1; i < num; i++)
                {
                    //format our string and append the duplicate key specified
                    stringBuilder.AppendFormat("&{0}=", _duplicateKey);
                    stringBuilder.Append((string)list[i]);
                }
                return stringBuilder.ToString();                   
            }
            return null;
        }
        else
           return base.Get(index);
    }

} 

您可以像普通的NameValueCollection一样使用它,但是如果在构造函数中传递重复的strn,它将查找该重复的键并运行上面的修改后的代码(否则它将仅使用默认的base.Get方法

You can use it like a normal NameValueCollection but if you pass in a duplicate strning in the constructor, it will look for that duplicate key and run the modified code above (otherwise it will just use the default base.Get method.

DupeNVC dnvc = new DupeNVC("formats");

foreach(var f in formats)     
    dnvc.Add("formats", f);

webClient.QueryString = dnvc;

此功能尚未经过全面测试,但应输出所需的查询字符串格式.当然,可以通过收集重复的键来进一步扩展此操作,但这只是为了让您对当前的问题有所了解.

This hasn't been fully tested but it should output the querystring format you want. Of course, this could be extended further by taking a collection of duplicate keys but this was just to give you an idea for your current problem.

这篇关于如何使用重复键构建WebClient查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆