http方式传递参数值转义或乱码的处理(base64)
如果通过http方式传递参数url编码了,可用urlEncode和urlDecode,这种方式不同开发语言编码出来的可能不同,所以不同开发语言最好用base64编码和解码来处理:
base64加密:
/// <summary>
/// Base64加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
private string EncodeBase64(string source)
{
string encode = "";
byte[] bytes = Encoding.UTF8.GetBytes(source);
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = source;
}
return encode;
}
base64解码:
/// <summary>
/// BASE64解码
/// </summary>
/// <param name="sourceStr"></param>
/// <returns></returns>
private static string Base64StringDecode(string sourceStr)
{
byte[] buf = Convert.FromBase64String(sourceStr);
return Encoding.UTF8.GetString(buf);
}