采用 Base64 编码不仅比较简短,同时具有不可读性,被编码的数据不会被人用肉眼所直接看到。 但是,标准的 Base64 并不适合直接放在 URL 里使用,因为Base64中含有'+' 和 '/', URL 编码器会 '+' 和 '/' 变为形如 "%XX” 的形式。
    修改一下Base64就可以在URL中使用了:
/// <summary>
/// Base64 转换 UrlBase64,使之适合在url中使用
/// </summary>
public static string Base64ToUrlBase64(string base64str)
{
    // "+" 换成 "-A"
    
// "/" 换成 "-S"
    
// 去掉 "="
    return base64str.Replace("+""-A").Replace("/""-S").Replace("="string.Empty);
}

/// <summary>
/// UrlBase64 转换 Base64
/// </summary>
public static string UrlBase64ToBase64(string urlBase64str)
{
    // "-A" 换成 "+"
    
// "-S" 换成 "/"
    string str = urlBase64str.Replace("-A""+").Replace("-S""/");

    // 添加"="
    int mod = str.Length % 4;
    if (mod != 0)
    {
        str += new string('='4 - mod);
    }
    return str;
}
怎么样,UrlBase64在URL使用很方便吧!

相关文章:

  • 2021-12-18
  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
  • 2021-07-16
  • 2021-10-28
猜你喜欢
  • 2022-01-22
  • 2022-12-23
  • 2021-08-31
  • 2021-11-11
  • 2021-11-18
  • 2021-11-17
  • 2022-12-23
相关资源
相似解决方案