runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  710 随笔 :: 0 文章 :: 127 评论 :: 98万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

HttpUtility.UrlEncode和Uri.EscapeDataString的区别V2024

 

先上代码:

 

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ConsoleUrlEncode
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "C++C#()";
            Console.WriteLine("HttpUtility.UrlEncode1:      " + HttpUtility.UrlEncode(url));
            Console.WriteLine("HttpUtility.UrlEncode 自定义:" + UrlEncode1(url));
            Console.WriteLine("Uri.EscapeDataString 自定义: " + UrlEncode2(url));
            Console.WriteLine("Uri.EscapeDataString:        " + Uri.EscapeDataString(url));
            Console.WriteLine("1");
            Console.ReadKey();
        }

        /// <summary>
        /// 使用 HttpUtility.UrlEncode,需要encode的字符转大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncode1(string str)
        {
            StringBuilder builder = new StringBuilder();
            foreach (char c in str)
            {
                if (HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).Length > 1)
                {
                    builder.Append(HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).ToUpper());
                }
                else
                {
                    builder.Append(c);
                }
                //if (Uri.EscapeDataString(c.ToString()).Length > 1)
                //{
                //    builder.Append(Uri.EscapeDataString(c.ToString()).ToUpper());
                //}
                //else
                //{
                //    builder.Append(c);
                //}

            }
            return builder.ToString();
        }

        /// <summary>
        /// 使用 Uri.EscapeDataString,需要encode的字符转大写
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string UrlEncode2(string str)
        {
            StringBuilder builder = new StringBuilder();
            foreach (char c in str)
            {
                //if (HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).Length > 1)
                //{
                //    builder.Append(HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).ToUpper());
                //}
                //else
                //{
                //    builder.Append(c);
                //}
                if (Uri.EscapeDataString(c.ToString()).Length > 1)
                {
                    builder.Append(Uri.EscapeDataString(c.ToString()).ToUpper());
                }
                else
                {
                    builder.Append(c);
                }

            }
            return builder.ToString();
        }
    }
}
复制代码

看下输出:

HttpUtility.UrlEncode1:      C%2b%2bC%23()
HttpUtility.UrlEncode 自定义:C%2B%2BC%23()
Uri.EscapeDataString 自定义: C%2B%2BC%23%28%29
Uri.EscapeDataString:        C%2B%2BC%23%28%29

 

 

使用HttpUtility.UrlEncode,会把要encdoe字符转义成小写,+号被转义成 %2b,而括号 () 不会被编码。 JAVA 那边默认风格是转大写,经常遇到报签名错误的,一般是这个原因引起的。

网上找的“public static string UrlEncode1(string str)”方法,只是把要编码的字符转成大写而已,而括号 () 不会被编码。

UrlEncode2 是仿UrlEncode1写的,只不过判定是否要编码,从HttpUtility.UrlEncode换成Uri.EscapeDataString。

UrlEncode2 和 直接使用 Uri.EscapeDataString 对整个字符编码,结果是一致的,还不如直接用 Uri.EscapeDataString,而不用 UrlEncode2方法。

 Uri.EscapeDataString:1.遇到要编码的,会转成大写。2.括号 () 也会被编码。

所以有遇到和其它语言进行签名交互时,直接用 Uri.EscapeDataString 对整个字符编码。 应该就可以验证通过。

 

posted on   runliuv  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2020-10-16 IDEA
2016-10-16 c#.net 调用BouncyCastle生成PEM格式的私钥和公钥
2013-10-16 SQL Server Management Studio (SSMS) 清除登录记录
点击右上角即可分享
微信分享提示