公开Request.QueryString实现方法

本文介绍了一个自定义的 QueryValueCollection 类,该类继承自 NameValueCollection 并用于处理 URL 编码的查询字符串。文章详细展示了如何从字符串中填充键值对,并提供了支持 URL 解码和指定编码的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Specialized;
using System.Text;

/// <summary>
/// QueryValueCollection 的摘要说明
/// </summary>
public class QueryValueCollection : NameValueCollection
{
    
//
    
// TODO: 在此处添加构造函数逻辑
    
//
    public QueryValueCollection(string str, bool readOnly, bool urlencoded, Encoding encoding)
    {
        
if (!string.IsNullOrEmpty(str))
        {
            
this.FillFromString(str, urlencoded, encoding);
        }
        
base.IsReadOnly = readOnly;
    }

    
public void FillFromString(string s, bool urlencoded, Encoding encoding)
    {
        
int num = (s != null? s.Length : 0;
        
for (int i = 0; i < num; i++)
        {
            
int startIndex = i;
            
int num4 = -1;
            
while (i < num)
            {
                
char ch = s[i];
                
if (ch == '=')
                {
                    
if (num4 < 0)
                    {
                        num4 
= i;
                    }
                }
                
else if (ch == '&')
                {
                    
break;
                }
                i
++;
            }
            
string str = null;
            
string text2 = null;
            
if (num4 >= 0)
            {
                str 
= s.Substring(startIndex, num4 - startIndex);
                text2 
= s.Substring(num4 + 1, (i - num4) - 1);
            }
            
else
            {
                text2 
= s.Substring(startIndex, i - startIndex);
            }
            
if (urlencoded)
            {
                
base.Add(HttpUtility.UrlDecode(str, encoding), HttpUtility.UrlDecode(text2, encoding));
            }
            
else
            {
                
base.Add(str, text2);
            }
            
if ((i == (num - 1)) && (s[i] == '&'))
            {
                
base.Add(nullstring.Empty);
            }
        }
    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值