zoukankan      html  css  js  c++  java
  • c# url自动解码解决方案

    昨天下午易宝维护人员联系我,询问我三笔订单的状态,他们是扣费成功了,我们这还是订单待扣费状态,经过检查发现双方的签名有问题,仔细一看这三笔订单都有一个共性,用户名都包含中文,然后就对症下药。。。

    由于易宝再扣费成功后通知我方前,对字符串用gb2312 urlencode了,而我方接口是用.net实现的,所以默认的编码格式是utf-8,所以接收到请求后就把字符串的内容用utf-8 urldecode了,导致我这边HttpUtility.UrlDecode(Request["user_name"], Encoding.GetEncoding("GB2312")) 获得到的数据就是乱码了。

    现在问题找到原因了,怎么修改呢。。。

    我查看了整个request对象,发现Request.Url.Query中,获得的参数是最原始的没有经过url解码前的值然后就这么修改了

     1 public static NameValueCollection GetQueryString(string queryString, Encoding encoding)
     2         {
     3             queryString = queryString.Replace("?", "");
     4             NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
     5             if (!string.IsNullOrEmpty(queryString))
     6             {
     7                 int count = queryString.Length;
     8                 for (int i = 0; i < count; i++)
     9                 {
    10                     int startIndex = i;
    11                     int index = -1;
    12                     while (i < count)
    13                     {
    14                         char item = queryString[i];
    15                         if (item == '=')
    16                         {
    17                             if (index < 0)
    18                             {
    19                                 index = i;
    20                             }
    21                         }
    22                         else if (item == '&')
    23                         {
    24                             break;
    25                         }
    26                         i++;
    27                     }
    28                     string key = null;
    29                     string value = null;
    30                     if (index >= 0)
    31                     {
    32                         key = queryString.Substring(startIndex, index - startIndex);
    33                         value = queryString.Substring(index + 1, (i - index) - 1);
    34                     }
    35                     else
    36                     {
    37                         key = queryString.Substring(startIndex, i - startIndex);
    38                     }
    39 
    40                     result[key] = HttpUtility.UrlDecode(value, encoding);
    41 
    42                     if ((i == (count - 1)) && (queryString[i] == '&'))
    43                     {
    44                         result[key] = string.Empty;
    45                     }
    46                 }
    47             }
    48             return result;
    49         }
    50 
    51      NameValueCollection col = GetQueryString(Request.Url.Query, Encoding.GetEncoding("GB2312"));
    52         string user_id = col["user_name"];
  • 相关阅读:
    Best Time to Buy and Sell Stock
    Remove Nth Node From End of List
    Unique Paths
    Swap Nodes in Pairs
    Convert Sorted Array to Binary Search Tree
    Populating Next Right Pointers in Each Node
    Maximum Subarray
    Climbing Stairs
    Unique Binary Search Trees
    Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/xuxzx/p/4251986.html
Copyright © 2011-2022 走看看