.net UrlEncode - lowercase problem

后端 未结 7 985
梦如初夏
梦如初夏 2020-11-27 19:56

I\'m working on a data transfer for a gateway which requires me to send data in UrlEncoded form. However, .net\'s UrlEncode creates lowercase tags, and it breaks the transfe

7条回答
  •  萌比男神i
    2020-11-27 20:45

    I think you're stuck with what C# gives you, and getting errors suggests a poorly implemented UrlDecode function on the other end.

    With that said, you should just need to loop through the string and uppercase only the two characters following a % sign. That'll keep your base64 data intact while massaging the encoded characters into the right format:

    public static string UpperCaseUrlEncode(string s)
    {
      char[] temp = HttpUtility.UrlEncode(s).ToCharArray();
      for (int i = 0; i < temp.Length - 2; i++)
      {
        if (temp[i] == '%')
        {
          temp[i + 1] = char.ToUpper(temp[i + 1]);
          temp[i + 2] = char.ToUpper(temp[i + 2]);
        }
      }
      return new string(temp);
    }
    

提交回复
热议问题