怎么把一个汉字转换为URLEncode格式,而不是用URLEncode()

解决方案 »

  1.   

    用javascript的enscape方法或者自己写个加密算法
      

  2.   

    winform里面URLEncode不能用吧?
    你可以在javascript里用encodeURLComponent或者enscape
      

  3.   

    winform里面可以用URLEncode!
    添加引用即可!

    反编译HttpServerUtility.UrlEncode
    后面是逐层调用的方法public string UrlEncode(string s)
    {
          Encoding encoding1 = (this._context != null) ? this._context.Response.ContentEncoding : Encoding.UTF8;
          return HttpUtility.UrlEncode(s, encoding1);
    }public static string UrlEncode(string str, Encoding e)
    {
          if (str == null)
          {
                return null;
          }
          return Encoding.ASCII.GetString(HttpUtility.UrlEncodeToBytes(str, e));
    }
    public static byte[] UrlEncodeToBytes(string str, Encoding e)
    {
          if (str == null)
          {
                return null;
          }
          byte[] buffer1 = e.GetBytes(str);
          return HttpUtility.UrlEncodeBytesToBytesInternal(buffer1, 0, buffer1.Length, false);
    }
    private static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue)
    {
          int num1 = 0;
          int num2 = 0;
          for (int num3 = 0; num3 < count; num3++)
          {
                char ch1 = (char) bytes[offset + num3];
                if (ch1 == ' ')
                {
                      num1++;
                }
                else if (!HttpUtility.IsSafe(ch1))
                {
                      num2++;
                }
          }
          if ((!alwaysCreateReturnValue && (num1 == 0)) && (num2 == 0))
          {
                return bytes;
          }
          byte[] buffer1 = new byte[count + (num2 * 2)];
          int num4 = 0;
          for (int num5 = 0; num5 < count; num5++)
          {
                byte num6 = bytes[offset + num5];
                char ch2 = (char) num6;
                if (HttpUtility.IsSafe(ch2))
                {
                      buffer1[num4++] = num6;
                }
                else if (ch2 == ' ')
                {
                      buffer1[num4++] = 0x2b;
                }
                else
                {
                      buffer1[num4++] = 0x25;
                      buffer1[num4++] = (byte) HttpUtility.IntToHex((num6 >> 4) & 15);
                      buffer1[num4++] = (byte) HttpUtility.IntToHex(num6 & 15);
                }
          }
          return buffer1;

     
      

  4.   

    呃...LZ说JS也不能用...
    那咋整...
      

  5.   

    那就自己写个UrlEncode了 还不是一样
      

  6.   

    to viena(维也纳nn):谢谢 知道能用,感觉很别扭,CS上用到HttpServerUtility
    我想问,能不能可以通过编码格式来产生,ENCODING类
      

  7.   

    我反编译HttpServerUtility.UrlEncode的源码就是给你参考它的实现方法的呀
      

  8.   

    System.Web.HttpUtility.UrlEncode