分享

UrlEncode编码 unity c#

 鸿蛟家平 2020-06-23

今天接入易接的SDK,在支付回调一直失败。问后原因是前端调用易接支付接口pay()中的callbackinfo需要进行urlEncode编码。

为什么需要用urlEncode编码?是因为当字符串数据以url的形式传递给web服务器时,字符串中是不允许出现空格和特殊字符的。

附Unity C#版 urlEncode编码 类

复制代码
  1 using System.Text;
  2 using System.Collections;
  3 public static class WebUtility
  4 {
  5     // Fields
  6 
  7     private static char[] _htmlEntityEndingChars = new char[] { ';', '&' };
  8     private const char HIGH_SURROGATE_START = '\ud800';
  9     private const char LOW_SURROGATE_END = '\udfff';
 10     private const char LOW_SURROGATE_START = '\udc00';
 11     private const int UNICODE_PLANE00_END = 0xffff;
 12     private const int UNICODE_PLANE01_START = 0x10000;
 13     private const int UNICODE_PLANE16_END = 0x10ffff;
 14     private const int UnicodeReplacementChar = 0xfffd;
 15 
 16 
 17 
 18     private static void ConvertSmpToUtf16(uint smpChar, out char leadingSurrogate, out char trailingSurrogate)
 19     {
 20         int num = ((int) smpChar) - 0x10000;
 21         leadingSurrogate = (char) ((num / 0x400) + 0xd800);
 22         trailingSurrogate = (char) ((num % 0x400) + 0xdc00);
 23     }
 24 
 25     private static int HexToInt(char h)
 26     {
 27         if ((h >= '0') && (h <= '9'))
 28         {
 29             return (h - '0');
 30         }
 31         if ((h >= 'a') && (h <= 'f'))
 32         {
 33             return ((h - 'a') + 10);
 34         }
 35         if ((h >= 'A') && (h <= 'F'))
 36         {
 37             return ((h - 'A') + 10);
 38         }
 39         return -1;
 40     }
 41 
 42 
 43 
 44 
 45 
 46     private static char IntToHex(int n)
 47     {
 48         if (n <= 9)
 49         {
 50             return (char) (n + 0x30);
 51         }
 52         return (char) ((n - 10) + 0x41);
 53     }
 54 
 55     private static bool IsUrlSafeChar(char ch)
 56     {
 57         if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
 58         {
 59             return true;
 60         }
 61         switch (ch)
 62         {
 63         case '(':
 64         case ')':
 65         case '*':
 66         case '-':
 67         case '.':
 68         case '_':
 69         case '!':
 70             return true;
 71         }
 72         return false;
 73     }
 74 
 75     public static string UrlEncode(string value)
 76     {
 77         if (value == null)
 78         {
 79             return null;
 80         }
 81         byte[] bytes = Encoding.UTF8.GetBytes(value);
 82         return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length, false));
 83     }
 84     private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count)
 85     {
 86         if ((bytes == null) && (count == 0))
 87         {
 88             return false;
 89         }
 90         if (bytes == null)
 91         {
 92             //throw new ArgumentNullException("bytes");
 93         }
 94         if ((offset < 0) || (offset > bytes.Length))
 95         {
 96             //throw new ArgumentOutOfRangeException("offset");
 97         }
 98         if ((count < 0) || ((offset + count) > bytes.Length))
 99         {
100             //throw new ArgumentOutOfRangeException("count");
101         }
102         return true;
103     }
104 
105 
106 
107     private static byte[] UrlEncode(byte[] bytes, int offset, int count)
108     {
109         if (!ValidateUrlEncodingParameters(bytes, offset, count))
110         {
111             return null;
112         }
113         int num = 0;
114         int num2 = 0;
115         for (int i = 0; i < count; i++)
116         {
117             char ch = (char) bytes[offset + i];
118             if (ch == ' ')
119             {
120                 num++;
121             }
122             else if (!IsUrlSafeChar(ch))
123             {
124                 num2++;
125             }
126         }
127         if ((num == 0) && (num2 == 0))
128         {
129             return bytes;
130         }
131         byte[] buffer = new byte[count + (num2 * 2)];
132         int num4 = 0;
133         for (int j = 0; j < count; j++)
134         {
135             byte num6 = bytes[offset + j];
136             char ch2 = (char) num6;
137             if (IsUrlSafeChar(ch2))
138             {
139                 buffer[num4++] = num6;
140             }
141             else if (ch2 == ' ')
142             {
143                 buffer[num4++] = 0x2b;
144             }
145             else
146             {
147                 buffer[num4++] = 0x25;
148                 buffer[num4++] = (byte) IntToHex((num6 >> 4) & 15);
149                 buffer[num4++] = (byte) IntToHex(num6 & 15);
150             }
151         }
152         return buffer;
153     }
154 
155     private static byte[] UrlEncode(byte[] bytes, int offset, int count, bool alwaysCreateNewReturnValue)
156     {
157         byte[] buffer = UrlEncode(bytes, offset, count);
158         if ((alwaysCreateNewReturnValue && (buffer != null)) && (buffer == bytes))
159         {
160             return (byte[]) buffer.Clone();
161         }
162         return buffer;
163     }
164 
165 
166     public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count)
167     {
168         return UrlEncode(value, offset, count, true);
169     }
170 
171 
172 
173 }
复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多