C# URL/HTML Encode与Decode

Encode

public static string UrlEncode(string str)
{
    StringBuilder sb = new StringBuilder();
    byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
    for (int i = 0; i < byStr.Length; i++)
    {
        sb.Append(@"%" + Convert.ToString(byStr[i], 16));//将 8 位无符号整数的值转换为其等效的指定基数的字符串表示形式。(url转成16进制等效值。范围:2、8、10、16)
    }
    return sb.ToString();
}

Encode & Decode

使用C#自带方法

HtmlEncode与HtmlDecode:转换html,转化回车和空格的功能,可以将textarea中输入的文本按照原样在html中显示。简单的理解,就是将特殊html的字符实体与字符相互转换。

参考

//原型
 public static unsafe void HtmlEncode(string value, TextWriter output) {
            if (value == null) {
                return;
            }
            if (output == null) {
                throw new ArgumentNullException("output");
            }

            int index = IndexOfHtmlEncodingChars(value, 0);
            if (index == -1) {
                output.Write(value);
                return;
            }

            Debug.Assert(0 <= index && index <= value.Length, "0 <= index && index <= value.Length");

            UnicodeEncodingConformance encodeConformance = HtmlEncodeConformance;
            int cch = value.Length - index;
            fixed (char* str = value) {
                char* pch = str;
                while (index-- > 0) {
                    output.Write(*pch++);
                }

                for (; cch > 0; cch--, pch++) {
                    char ch = *pch;
                    if (ch <= '>') {
                        switch (ch) {
                            case '<':
                                output.Write("&lt;");
                                break;
                            case '>':
                                output.Write("&gt;");
                                break;
                            case '"':
                                output.Write("&quot;");
                                break;
                            case '\'':
                                output.Write("&#39;");
                                break;
                            case '&':
                                output.Write("&amp;");
                                break;
                            default:
                                output.Write(ch);
                                break;
                        }
                    }
                    else {
                        int valueToEncode = -1; // set to >= 0 if needs to be encoded

#if ENTITY_ENCODE_HIGH_ASCII_CHARS
                        if (ch >= 160 && ch < 256) {
                            // The seemingly arbitrary 160 comes from RFC
                            valueToEncode = ch;
                        } else
#endif // ENTITY_ENCODE_HIGH_ASCII_CHARS
                        if (encodeConformance == UnicodeEncodingConformance.Strict && Char.IsSurrogate(ch)) {
                            int scalarValue = GetNextUnicodeScalarValueFromUtf16Surrogate(ref pch, ref cch);
                            if (scalarValue >= UNICODE_PLANE01_START) {
                                valueToEncode = scalarValue;
                            }
                            else {
                                // Don't encode BMP characters (like U+FFFD) since they wouldn't have
                                // been encoded if explicitly present in the string anyway.
                                ch = (char)scalarValue;
                            }
                        }

                        if (valueToEncode >= 0) {
                            // value needs to be encoded
                            output.Write("&#");
                            output.Write(valueToEncode.ToString(NumberFormatInfo.InvariantInfo));
                            output.Write(';');
                        }
                        else {
                            // write out the character directly
                            output.Write(ch);
                        }
                    }
                }
            }
        }
//Html编码
System.Web.HttpUtility.HtmlEncode(str);
//Html解码
System.Web.HttpUtility.HtmlDecode(str);

//Url编码
System.Web.HttpUtility.UrlEncode(str);
System.Web.HttpUtility.UrlEncode(str,System.Text.Encoding.Unicode); 
//Url解码
System.Web.HttpUtility.UrlDecode(str);
System.Web.HttpUtility.UrlDecode(str,System.Text.Encoding.Unicode); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值