工作上和另一个公司对接,调对方的api需要用到md5加密,加密前要使用HttpUtility.UrlEncode,对方接口一直返回验证错误,定位了问题发现是中文编码使用HttpUtility.UrlEncode导致的。

C#中HttpUtility.UrlEncode返回的url编码是小写的,但对方是大写的,md5之后得到的hash码就不一致了,所以需要对HttpUtility.UrlEncode方法进行一层封装,代码如下:

        public static string UrlEncode(string str)
{
string urlStr = HttpUtility.UrlEncode(str);
var urlCode = Regex.Matches(urlStr, "%[a-f0-9]{2}", RegexOptions.Compiled).Cast<Match>().Select(m => m.Value).Distinct();
foreach (string item in urlCode)
{
urlStr = urlStr.Replace(item, item.ToUpper());
}
return urlStr;
}

目前的想法是这样的,有空再来优化一下。

更新:想过其他方案,似乎这样正则匹配反而降低了效率,用foreach对每个字符进行UrlEncode似乎才是正解,实测效率也比正则匹配后的效率高。下面是代码:

        public static string UrlEncode1(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if (HttpUtility.UrlEncode(c.ToString()).Length > )
{
sb.Append(HttpUtility.UrlEncode(c.ToString()).ToUpper());
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}

C#HttpUtility.UrlEncode 大写问题的更多相关文章

  1. C#实现HttpUtility.UrlEncode输出大写字母

    在c#中,HttpUtility.UrlEncode("www+mzwu+com")编码结果为www%2bmzwu%2bcom,在和Java开发的平台做对接的时候,对方用用url编 ...

  2. HttpUtility.UrlEncode编码重写

    1. 某些系统方法,例如.NET系统方法HttpUtility.UrlEncode会将‘=’编码成‘%3d’,而不是%3D,导致加密签名通不过验证,请开发者注意检查. 2.Java 1.3和早期版本中 ...

  3. C#中的System.Web.HttpUtility.UrlEncode转码问题

    最近需要与JAVA程序对接口,其中遇到的URL转码问题: Java中URL编码所用的 URLEncoder.encode 产生的字符是大写且英文'(',')'是分别转成'%28'和 '%29' 而C# ...

  4. Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办?

    Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办? 编码: Uri.EscapeDataString(name) 解码: ...

  5. HttpUtility.UrlEncode 和Server.UrlEncode的区别,记录记录,被乱码搞晕头了。。。。。

    今天修改原来的站点,有几个session和cookies乱码问题,然后又好好看了一下关于编码这块的内容. 大概是:登录处,用session记录了一点中文内容.然后cookies也记录了一点中文内容,取 ...

  6. asp.net4.0在Global中的Application_Start 中直接或间接使用 HttpUtility.UrlEncode等出现异常Response is not available in this context的解决方法

    HttpUtility.HtmlEncode HttpUtility.HtmlDecode HttpUtility.UrlEncode HttpUtility.UrlDecode 也会出现此异常. 这 ...

  7. HttpUtility.UrlEncode()关于空格的编码问题

    因为 HttpUtility.UrlEncode 在 Encode 的时候, 将空格转换成加号"+", 在 Decode 的时候将"+"号转为空格, 但是浏览器 ...

  8. c# http get post转义HttpUtility.UrlEncode

    //该数据如果要http get.post提交,需要经过转义,否则该数据中含& ''等字符会导致意外错误.需要转义.这里用HttpUtility.UrlEncode来转义.接收方无需反解析 s ...

  9. c#asp.net url 传递中文参数要使用 System.Web.HttpUtility.UrlEncode 而不能使用Server.UrlEncode

    最近网站里的参数包括中文的例如: http://www.taiba/Tag%b0%ae%c7%e9.html 已开始使用 Server.UrlEncode来做的,但发现,有一些中文在url重写的是说找 ...

随机推荐

  1. 【英宝通Unity4.0公开课学习 】(一)资源管理

    经过多次面试后发现自己对Unity3D的框架缺乏一个整体的认识. 而前面由于离职等原因总是忙于修修补补,疲于奔命,感觉相当疲惫. 还好,后来经过调整,开始淡定了起来.得特别感谢一本书哇:<高效人 ...

  2. SVN概述

    ----------------------siwuxie095                                 SVN 概述         1.SVN 即 Subversion 的 ...

  3. [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  4. TokuMX唯一索引不支持dropDups选项

    TokuMX v1.5.0的唯一索引(unique index)不支持dropDups选项, 如果源数据包含相同目标key的文档,将无法建立唯一索引. 问题场景: 从MongoDB到TokuMX的数据 ...

  5. 06 Maven 聚合和继承

    Maven 聚合和继承 1. 聚合 2. 继承 <parent> <groupId>org.apache.karaf.demos</groupId> <art ...

  6. 设计模式-生成者模式之c#代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. 继续修改爬虫百度贴吧,这次随意贴吧的任何一个index页都行,然后自动d盘生成tupian文件夹来保存

    from urllib.request import urlopenfrom bs4 import BeautifulSoupfrom urllib.request import urlopenimp ...

  8. attempt to create delete event with null entity

    解决办法:删除之前判断是否为空 if(Object != null){ session.delete(Object); }

  9. Activiti中23张表的含义

    1.与流程定义相关的4张表: 2.与执行任务相关的5张表: 3.与流程变量相关的2张表

  10. C#与android连接 SimpleWifi

    有时候 Read时会返回0长度 ----- 当连续2次每读到数据时,建议发个心跳信息,然后单片机给个回复 C# using System; using System.Collections.Gener ...