示例#1
0
            private string ToString(bool urlencoded, IDictionary excludeKeys)
            {
                int n = Count;

                if (n == 0)
                {
                    return(string.Empty);
                }

                StringBuilder s = new StringBuilder();
                string        key, keyPrefix, item;

                for (int i = 0; i < n; i++)
                {
                    key = GetKey(i);

                    if (excludeKeys != null && key != null && excludeKeys[key] != null)
                    {
                        continue;
                    }
                    if (urlencoded)
                    {
                        key = UrlUtility.UrlEncodeUnicode(key);
                    }
                    keyPrefix = (!string.IsNullOrEmpty(key)) ? (key + "=") : string.Empty;

                    ArrayList values    = (ArrayList)BaseGet(i);
                    int       numValues = (values != null) ? values.Count : 0;

                    if (s.Length > 0)
                    {
                        s.Append('&');
                    }

                    if (numValues == 1)
                    {
                        s.Append(keyPrefix);
                        item = (string)values[0];
                        if (urlencoded)
                        {
                            item = UrlUtility.UrlEncodeUnicode(item);
                        }
                        s.Append(item);
                    }
                    else if (numValues == 0)
                    {
                        s.Append(keyPrefix);
                    }
                    else
                    {
                        for (int j = 0; j < numValues; j++)
                        {
                            if (j > 0)
                            {
                                s.Append('&');
                            }
                            s.Append(keyPrefix);
                            item = (string)values[j];
                            if (urlencoded)
                            {
                                item = UrlUtility.UrlEncodeUnicode(item);
                            }
                            s.Append(item);
                        }
                    }
                }

                return(s.ToString());
            }
示例#2
0
            internal void FillFromString(string s, bool urlencoded, Encoding encoding)
            {
                int l = (s != null) ? s.Length : 0;
                int i = 0;

                while (i < l)
                {
                    // find next & while noting first = on the way (and if there are more)

                    int si = i;
                    int ti = -1;

                    while (i < l)
                    {
                        char ch = s[i];

                        if (ch == '=')
                        {
                            if (ti < 0)
                            {
                                ti = i;
                            }
                        }
                        else if (ch == '&')
                        {
                            break;
                        }

                        i++;
                    }

                    // extract the name / value pair

                    string name  = null;
                    string value = null;

                    if (ti >= 0)
                    {
                        name  = s.Substring(si, ti - si);
                        value = s.Substring(ti + 1, i - ti - 1);
                    }
                    else
                    {
                        value = s.Substring(si, i - si);
                    }

                    // add name / value pair to the collection

                    if (urlencoded)
                    {
                        base.Add(
                            UrlUtility.UrlDecode(name, encoding),
                            UrlUtility.UrlDecode(value, encoding));
                    }
                    else
                    {
                        base.Add(name, value);
                    }

                    // trailing '&'

                    if (i == l - 1 && s[i] == '&')
                    {
                        base.Add(null, string.Empty);
                    }

                    i++;
                }
            }