在我的android应用程序中,我使用的是java.util.Base64编码和解码,标志是URL_SAFE和NO_WRAP。
但是,当我试图在我的C#应用程序中使用HttpServerUtility.UrlTokenEncode解码它时,我将返回null。在这种状态下,我的编码字符串也不能在Android应用程序上解码。
我错过了什么?URL_SAFE标志不确保Base64字符串没有+、/和任何额外的填充吗?为什么UrlTokenEncode不接受Base64值?
I was using this post as reference, for anyone who is interested.
发布于 2016-05-06 16:35:52
UrlTokenEncode返回null是因为我传递的是一个string,而不是一个UrlToken。
坚持使用URL_SAFE和NO_WRAP Base64标志进行编码/解码,我设法将NO_WRAP应用程序更改为以url_safe方式解码/编码。
public string UrlEncode(string str)
{
if (str == null || str == "")
{
return null;
}
byte[] bytesToEncode = System.Text.UTF8Encoding.UTF8.GetBytes(str);
String returnVal = System.Convert.ToBase64String(bytesToEncode);
return returnVal.TrimEnd('=').Replace('+', '-').Replace('/', '_');
}
public string UrlDecode(string str)
{
if (str == null || str == "")
{
return null;
}
str.Replace('-', '+');
str.Replace('_', '/');
int paddings = str.Length % 4;
if (paddings > 0)
{
str += new string('=', 4 - paddings);
}
byte[] encodedDataAsBytes = System.Convert.FromBase64String(str);
string returnVal = System.Text.UTF8Encoding.UTF8.GetString(encodedDataAsBytes);
return returnVal;
}https://stackoverflow.com/questions/37063077
复制相似问题