蝈蝈俊.net

-- 用随笔来记录自己的技术感触
随笔 - 597, 评论 - 4064, 引用 - 276

导航

关于

这里是我的技术Blog,下一代CSDN社区Blog在 http://blog.csdn.net/ghj1976/

标签

每月存档

最新留言

  • re:学习笔记:7种结构型设计模式简单对比
    <p>最新在家创业系统 ----刚从国外引进,市场巨大。 ----在家可经营所有国家生意,事业规模宏大。 ----不需求人与说服;不用放厚脸皮去推销。 ----极小投资;零风险;成...
    by jackielongteng(注册) on 2009/6/14 13:43:56
  • re:作用域
    <p>☆                    &deg;∵☆       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...
    by jackielongteng(注册) on 2009/6/14 13:03:25
  • re:Html标签嵌套对展示性能的影响
    <p><strong>所有的浏览器都按照你提到的浏览器解析Html规则来解析嘛?</strong></p>
    by Cola(注册) on 2009/6/12 23:07:28
  • re:Html5
    <p>目前来说,HTML5还只是一个梦想,呵呵:)</p>
    by 开心就好(注册) on 2009/6/11 16:31:54
  • re:多线程与SqlConnection.Close
    <p>好服月租型IT服务台,与你共成长! 月租型ITSM软件,注册即可免费体验! 详情请登录官方网站:<a href="http://www.servicezon.co...
    by qzhibo(注册) on 2009/6/3 15:14:21
  • re:多线程Singleton单件模式
    <pre><span style="color: #0000ff;">//Another way public</span> <sp...
    by Yaojian(注册) on 2009/4/22 14:02:35
  • re:Thread.Sleep(0)
    <p>学习了~</p>
    by shuitong888(注册) on 2009/4/8 14:29:24
  • re:Html标签嵌套对展示性能的影响
    <p>DIV固然好 但IE6,7,8 firefox,safari ....做美工的人要累死.</p>
    by ryq1(注册) on 2009/4/3 14:16:25
  • re:用.net 编码实现朗读文本的方法
    <p>我第一次 按键时 能听到声音,但是第二次按键时,没反应。网页一直在 loading.&nbsp;是什么原因?</p>
    by tracytang949(注册) on 2009/3/27 7:01:09
  • re:information_schema.routines与sysobjects
    <p>用sys.procedures多好。</p>
    by luke(注册) on 2009/3/16 16:45:49
  • re:SQL Server 2005 配置发送邮件
    <p>&lt;A href="<a href="http://www.3rt.info">http://www.3rt.info</...
    by ives007(注册) on 2009/2/26 16:47:00
  • re:推荐 Gemini 这个bug管理工具
    <p>你好!首先非常感谢推荐使用Gemini,这段时间在使用Gemini,有些问题想请教以下。</p> <p>1.Create Issue 以后,设置了Visib...
    by CowboyRyan(注册) on 2009/2/20 15:45:08
  • re:推荐 Gemini 这个bug管理工具
    <p>你好!首先非常感谢推荐使用Gemini,这段时间在使用Gemini,有些问题想请教以下。</p> <p>1.Create Issue 以后,设置了Visib...
    by CowboyRyan(注册) on 2009/2/20 15:32:06
  • re:虚机搭配NLB负载平衡时碰到"没有接口可用于安装新的群集"的解决方案
    <p>google newsid</p>
    by iads(注册) on 2009/2/13 17:25:07
  • re:try catch 与线程
    <p>确实是这样的。因为异常机制本质上是堆栈操作,而各线程的堆栈是独立的。</p>
    by st_szr(注册) on 2009/1/21 9:46:05
  • re:try catch 与线程
    <p>没啥啊,线程就是新启动了一个,当然异常不会影响到原有的线程了。</p> <p>你应该在线程里面合适的位置写上自己的捕获代码就行了。</p>
    by laozizhu(注册) on 2009/1/19 16:33:21
  • re:我的2008,征服天堂
    <p>蝈蝈,可惜我帮不了你啊!</p>
    by laozizhu(注册) on 2009/1/19 16:25:45
  • re:try catch 与线程
    <p>呃&hellip;&hellip;是这样的。可怎么处理呢?</p>
    by Anders Liu(注册) on 2009/1/19 11:58:05
  • re:我的2008,征服天堂
    <p>博主是不是去了师部 做了侦查营长呢?</p>
    by huobazi(注册) on 2009/1/9 14:15:33
  • re:我的2008,征服天堂
    <p>@ghj1976:看来真的危机了</p>
    by 开心就好(注册) on 2009/1/9 10:17:37
  • re: 网络带宽的单位
    不过传输的时候,往往还有压缩。
    by luke(匿名) on 2008/12/15 11:00:21
  • re: 网络带宽的单位
    除10不仅仅是为了方便,在传输中,往往加上控制位,所以一个字节往往需要10Bit.
    by 关门放狗(匿名) on 2008/12/13 16:01:30
  • re: 多缓存并存
    对跨进程甚至跨服务器缓存的性能比较怀疑,进程通信和跨服务器通信代价不菲。即使有已有进程外数据可用,如果考虑在进程做份缓存,定期再进程间同步是否更佳?
    by jinglecat(匿名) on 2008/12/12 18:00:05
  • re: 网络带宽的单位
    好像还有一个为了方便换算,厂家使用的是 除10的处理方式的说法:于是100Mb/sec = 100M / 10 = 10M Byte/sec 所以我通常都是用除10而不是除8来做运算的。
    by kentliu(匿名) on 2008/12/11 11:38:55
  • re: 网络带宽的单位
    又不是大S小s
    by luke(匿名) on 2008/12/10 12:04:50

广告

 
在 System.Web 命名空间 下有个静态方法:

HttpUtility.ParseQueryString (String, Encoding)
使用指定的 Encoding 将查询字符串分析成一个 NameValueCollection。 
我们如下代码显示的是我们需要的东西:

string query = "?searchtype=1&bigclassid=0&searchKeys=123+456&author=&tabletype=now";
NameValueCollection nv = HttpUtility.ParseQueryString(query);
Console.WriteLine(nv["tabletype"]);

但是我们用 Reflector 去看这个函数的实现:
其中调用到了 System.Web.HttpValueCollection 类的
internal void FillFromString(string s, bool urlencoded, Encoding encoding); 方法

从 Reflector 看这个函数的实现如下:

internal void FillFromString(string s, bool urlencoded, Encoding encoding)
{
      int num1 = (s != null) ? s.Length : 0;
      for (int num2 = 0; num2 < num1; num2++)
      {
            int num3 = num2;
            int num4 = -1;
            while (num2 < num1)
            {
                  switch (s[num2])
                  {
                        case '=':
                              if (num4 < 0)
                              {
                                    num4 = num2;
                              }
                              break;
                  }
                  num2++;
            }
            string text1 = null;
            string text2 = null;
            if (num4 >= 0)
            {
                  text1 = s.Substring(num3, num4 - num3);
                  text2 = s.Substring(num4 + 1, (num2 - num4) - 1);
            }
            else
            {
                  text2 = s.Substring(num3, num2 - num3);
            }
            if (urlencoded)
            {
                  base.Add(HttpUtility.UrlDecode(text1, encoding), HttpUtility.UrlDecode(text2, encoding));
            }
            else
            {
                  base.Add(text1, text2);
            }
            if ((num2 == (num1 - 1)) && (s[num2] == '&'))
            {
                  base.Add(null, string.Empty);
            }
      }
}

 
分析代码我们就可以知道:它没有处理参数中,多个参数的问题。
如果我们把这个代码复制出来执行,它是无法把?searchtype=1&bigclassid=0&searchKeys=123+456&author=&tabletype=now 这样QueryString 中的后面几个参数解析出来的。

怪异的问题就在这里,
执行出来的效果是分析出来了,而分析代码却没有分析出来。


这个问题,也有其他人看到:
比如:http://codebetter.com/blogs/karlseguin/archive/2006/05/04/144052.aspx
不过他提供的解析函数,忽略考虑了一种情况:如果参数中出现如下的情况就会报错
from=/Expert/search.asp?bigclassid=61

所以我在用他的函数时候,对他的一个函数作了修改:
public static NameValueCollection ParseQueryString(string query, char delimiter)
{
    if (delimiter == '\0')
    {
        throw new ArgumentNullException("delimiter");
    }
    NameValueCollection queryString = new NameValueCollection();
    if (query == null || query.Length == 0)
    {
        return queryString;
    }
    if (query.StartsWith("?"))
    {
        query = query.Substring(1, query.Length - 1);
    }

    string[] pairs = query.Split(delimiter);
    foreach (string pair in pairs)
    {
        int tt = pair.IndexOf('=');
        if (tt <= 0)
            continue;

        string name = null, val = null;
        name = pair.Substring(0, tt);
        if (pair.Length > tt)
            val = pair.Substring(tt + 1);
        queryString.Add(name, val);
    }
    return queryString;
}    
    

至于 Reflector  分析出来的代码,经过请教几个牛人,最后认为是 Reflector  分析代码出错误了。出错就在 switch 函数那里,
1、 其中的 case '=': 的break 是退出 switch 而不是退出循环
2、 估计其中少了一个 case '&'
请教 装配脑袋 后,觉得正确地分析出来的代码应该是:

internal void FillFromString(string s, bool urlencoded, Encoding encoding)
{
    int num1 = (s != null) ? s.Length : 0;
    for (int num2 = 0; num2 < num1; num2++)
    {
        int num3 = num2;
        int num4 = -1;
        while (num2 < num1)
        {
            switch (s[num2])
            {
                case '=':
                    if (num4 < 0)
                    {
                        num4 = num2;
                    }
                    break;
                case '&':
                    goto BREAKWHILE;
            }
            num2++;
        }

        BREAKWHILE:
        string text1 = null;
        string text2 = null;
        if (num4 >= 0)
        {
            text1 = s.Substring(num3, num4 - num3);
            text2 = s.Substring(num4 + 1, (num2 - num4) - 1);
        }
        else
        {
            text2 = s.Substring(num3, num2 - num3);
        }
        if (urlencoded)
        {
            base.Add(HttpUtility.UrlDecode(text1, encoding), HttpUtility.UrlDecode(text2, encoding));
        }
        else
        {
            base.Add(text1, text2);
        }
        if ((num2 == (num1 - 1)) && (s[num2] == '&'))
        {
            base.Add(null, string.Empty);
        }
    }

}


打印 | 张贴于 2006-12-06 10:54:00 | Tag:.net 编程心得  技术随笔  VS2005相关

留言反馈

# 使用 Request.QueryString 接受参数时,跟编码有关的一些问题 编辑
我们先来看以下几个请求,看a.aspx页面用Request.QueryString接受到的是啥信息?页面URLRequest.QueryString[
2007-12-17 12:24:00 | [匿名用户:csdnexpert]
#使用 Request.QueryString 接受参数时,跟编码有关的一些问题 编辑

我们先来看以下几个请求,看a.aspx 页面用Request.QueryString接受到的是啥信息?

页面URL Request.QueryString[
2007-12-15 23:45:00 | [匿名用户:fredlau]
#回复: 使用 Reflector ; 查看代码时候,碰到的一个Reflector 的bug 编辑
经过强混淆的代码在Reflector里经常会出错显示不出
但运行都没问题的
2006-12-21 17:52:00 | [匿名用户:yezie]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.1.8