解码转码---System.web.HttpUtility 对象分析

使用的多是System.Web.Util.HttpEncoder对象。

分析System.Web.Util.HttpEncoder类。  HttpEncoder多使用System.Web.Util.HttpEncoderUtility类对象。



public static int HexToInt(char h)
{
    if ((h >= '0') && (h <= '9'))
    {
        return (h - '0');
    }
    if ((h >= 'a') && (h <= 'f'))
    {
        return ((h - 'a') + 10);
    }
    if ((h >= 'A') && (h <= 'F'))
    {
        return ((h - 'A') + 10);
    }
    return -1;
} 


public static char IntToHex(int n)
{ if (n <= 9)
    { return (char) (n + 0x30);
    } return (char) ((n - 10) + 0x61);
}

public static bool IsUrlSafeChar(char ch)
{ if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
    { return true;
    } switch (ch)
    { case '(': case ')': case '*': case '-': case '.': case '_': case '!': return true;
    } return false;
}

internal static string UrlEncodeSpaces(string str)
{ if ((str != null) && (str.IndexOf(' ') >= 0))
    { str = str.Replace(" ", "%20");
    } return str;
}

public static unsafe void HtmlEncode(string value, TextWriter output)
{ if (value != null)
    { if (output == null)
        { throw new ArgumentNullException("output");
        } int num = IndexOfHtmlEncodingChars(value, 0); if (num == -1)
        { output.Write(value);
        } else { int num2 = value.Length - num; fixed (char* str = ((char*) value))
            { char* chPtr = str; char* chPtr2 = chPtr; while (num-- > 0)
                { chPtr2++; output.Write(chPtr2[0]);
                } while (num2-- > 0)
                { chPtr2++; char ch = chPtr2[0]; if (ch <= '>')
                    { switch (ch)
                        { case '&':
                            { output.Write("&amp;"); continue;
                            } case '\'':
                            { output.Write("&#39;"); continue;
                            } case '"':
                            { output.Write("&quot;"); continue;
                            } case '<':
                            { output.Write("&lt;"); continue;
                            } case '>':
                            { output.Write("&gt;"); continue;
                            }
                        } output.Write(ch); continue;
                    } if ((ch >= '\x00a0') && (ch < 'Ā'))
                    { output.Write("&#"); output.Write(ch.ToString(NumberFormatInfo.InvariantInfo)); output.Write(';');
                    } else { output.Write(ch);
                    }
                }
            }
        }
    }
}

protected internal virtual byte[] UrlEncode(byte[] bytes, int offset, int count)
{ if (!ValidateUrlEncodingParameters(bytes, offset, count))
    { return null;
    } int num = 0; int num2 = 0; for (int i = 0; i < count; i++)
    { char ch = (char) bytes[offset + i]; if (ch == ' ')
        { num++;
        } else if (!HttpEncoderUtility.IsUrlSafeChar(ch))
        { num2++;
        }
    } if ((num == 0) && (num2 == 0))
    { return bytes;
    } byte[] buffer = new byte[count + (num2 * 2)]; int num4 = 0; for (int j = 0; j < count; j++)
    { byte num6 = bytes[offset + j]; char ch2 = (char) num6; if (HttpEncoderUtility.IsUrlSafeChar(ch2))
        { buffer[num4++] = num6;
        } else if (ch2 == ' ')
        { buffer[num4++] = 0x2b;
        } else { buffer[num4++] = 0x25; buffer[num4++] = (byte) HttpEncoderUtility.IntToHex((num6 >> 4) & 15); buffer[num4++] = (byte) HttpEncoderUtility.IntToHex(num6 & 15);
        }
    } return buffer;
}




protected internal virtual string UrlPathEncode(string value)
{ if (string.IsNullOrEmpty(value))
    { return value;
    } int index = value.IndexOf('?'); if (index >= 0)
    { return (this.UrlPathEncode(value.Substring(0, index)) + value.Substring(index));
    } return HttpEncoderUtility.UrlEncodeSpaces(this.UrlEncodeNonAscii(value, Encoding.UTF8));
}

注意看这里。这就是差别。

int index = value.IndexOf('?');
    if (index >= 0)
    {
        return (this.UrlPathEncode(value.Substring(0, index)) + value.Substring(index));
    }
    return HttpEncoderUtility.UrlEncodeSpaces(this.UrlEncodeNonAscii(value, Encoding.UTF8));
如果是path方法, 如果?号有任何值,则不再进行操作。直接传递。

转载于:https://my.oschina.net/mahaisong/blog/155611

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值