Asp.Net 编码和解码
最近因为项目需要,做了一个投票的页面(Html,比如A 页面),要把它Post到一个Aspx页面(比如B页面),在这个Aspx页面上,需要确认一下,在提交到数据库,可是问题出来了,用户在A页面上点击Submit,Post到B页面上的时候,在B页面用Request.Form[“ID”],接收,但是显示时,有时候是乱码,有的时候却是正常的,不知道为什么,在网上查了一些资料,看了一些编码的文章,感觉出现问题的原因是这样的,A页面虽然在开始有一句<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />,但是用户用的机器上的编码可能是UTF或者GB的,在用户Post的时候,是用用户自己上的编码格式编的码(我的理解),从而在Request的时候就已经成乱码了。一开始,我想两种办法,一种是用Url编码,比如我们在A页面上有一个Input : <input type="radio" id="IsRightSee_2" name="radiobuttonIsRightSee" value="音、视频播放不连续" />音、视频播放不连续,我们把它的Value用Url编码,变成<input type="radio" id="IsRightSee_2" name="radiobuttonIsRightSee" value="%e%d..之类的" />音、视频播放不连续,在B页面用Request[“radiobuttonIsRightSee”],得到Value,然后再对这个Value解码,可是出乎我的意料的是,用这个办法得到的也是乱码。碰壁后只有用第二种方案,就是用Unicode对其编码,因为原先我没有遇到编码解码问题,所以就在网上搜索资源,但是与其相关的都没有一个好的解决方案,只搜到一个汉字转Unicode的工具,把其下载下来,先做了一个测试,对上文中的” 音、视频播放不连续”,进行编码,得到“\u97F 3\u3001\u89C 6\u9891\u64AD\u653E\u4E0D\u8FDE\u7EED”,然后再B页面上在对这个Value解码,果然,没有成乱码,看到这得你会不会想到,我怎么对这一串进行解码的,这个工具又没有源代码,其实这个工具是用.Net写的,.Net写的我们就有办法看里边的源代码了,只要他没有对源代码加密,用Reflector就行。解码的源代码如下:
写到这里问题基本上解决了,可是如果您的页面上有n多的input ,你还用这个工具一个input,一个input,把其中的Value Copy –Convert-Parse,到你的页面上吗?其实我们可以写一个正则表达式,用正则表达式来找出input中的Value,然后编码之后,在把原先的Value替换成编码后的Value,这样的话,即省了功夫,又不会出错(除非你的正则有问题),如果你不清楚怎么写的话,见一下代码:
编码工具下载
private string NormalU2C(string input)
{
string str = "";
char[] chArray = input.ToCharArray();
Encoding bigEndianUnicode = Encoding.BigEndianUnicode;
for (int i = 0; i < chArray.Length; i++)
{
char ch = chArray[i];
if (ch.Equals('\\'))
{
i++;
i++;
char[] chArray2 = new char[4];
int index = 0;
index = 0;
while ((index < 4) && (i < chArray.Length))
{
chArray2[index] = chArray[i];
index++;
i++;
}
if (index == 4)
{
try
{
str = str + this.UnicodeCode2Str(chArray2);
}
catch (Exception)
{
str = str + @"\u";
for (int j = 0; j < index; j++)
{
str = str + chArray2[j];
}
}
i--;
}
else
{
str = str + @"\u";
for (int k = 0; k < index; k++)
{
str = str + chArray2[k];
}
}
}
else
{
str = str + ch.ToString();
}
}
return str;
}
private string UnicodeCode2Str(char[] u4)
{
if (u4.Length < 4)
{
throw new Exception("It's not a unicode code array");
}
string str = "0123456789ABCDEF";
char ch = char.ToUpper(u4[0]);
char ch2 = char.ToUpper(u4[1]);
char ch3 = char.ToUpper(u4[2]);
char ch4 = char.ToUpper(u4[3]);
int index = str.IndexOf(ch);
int num2 = str.IndexOf(ch2);
int num3 = str.IndexOf(ch3);
int num4 = str.IndexOf(ch4);
if (((index == -1) || (num2 == -1)) || ((num3 == -1) || (num4 == -1)))
{
throw new Exception("It's not a unicode code array");
}
byte num5 = (byte)(((index * 0x10) + num2) & 0xff);
byte num6 = (byte)(((num3 * 0x10) + num4) & 0xff);
byte[] bytes = new byte[] { num5, num6 };
return Encoding.BigEndianUnicode.GetString(bytes);
}
{
string str = "";
char[] chArray = input.ToCharArray();
Encoding bigEndianUnicode = Encoding.BigEndianUnicode;
for (int i = 0; i < chArray.Length; i++)
{
char ch = chArray[i];
if (ch.Equals('\\'))
{
i++;
i++;
char[] chArray2 = new char[4];
int index = 0;
index = 0;
while ((index < 4) && (i < chArray.Length))
{
chArray2[index] = chArray[i];
index++;
i++;
}
if (index == 4)
{
try
{
str = str + this.UnicodeCode2Str(chArray2);
}
catch (Exception)
{
str = str + @"\u";
for (int j = 0; j < index; j++)
{
str = str + chArray2[j];
}
}
i--;
}
else
{
str = str + @"\u";
for (int k = 0; k < index; k++)
{
str = str + chArray2[k];
}
}
}
else
{
str = str + ch.ToString();
}
}
return str;
}
private string UnicodeCode2Str(char[] u4)
{
if (u4.Length < 4)
{
throw new Exception("It's not a unicode code array");
}
string str = "0123456789ABCDEF";
char ch = char.ToUpper(u4[0]);
char ch2 = char.ToUpper(u4[1]);
char ch3 = char.ToUpper(u4[2]);
char ch4 = char.ToUpper(u4[3]);
int index = str.IndexOf(ch);
int num2 = str.IndexOf(ch2);
int num3 = str.IndexOf(ch3);
int num4 = str.IndexOf(ch4);
if (((index == -1) || (num2 == -1)) || ((num3 == -1) || (num4 == -1)))
{
throw new Exception("It's not a unicode code array");
}
byte num5 = (byte)(((index * 0x10) + num2) & 0xff);
byte num6 = (byte)(((num3 * 0x10) + num4) & 0xff);
byte[] bytes = new byte[] { num5, num6 };
return Encoding.BigEndianUnicode.GetString(bytes);
}
写到这里问题基本上解决了,可是如果您的页面上有n多的input ,你还用这个工具一个input,一个input,把其中的Value Copy –Convert-Parse,到你的页面上吗?其实我们可以写一个正则表达式,用正则表达式来找出input中的Value,然后编码之后,在把原先的Value替换成编码后的Value,这样的话,即省了功夫,又不会出错(除非你的正则有问题),如果你不清楚怎么写的话,见一下代码:
protected void Button1_Click(object sender, EventArgs e)
{
string strPatter = @"(<input\s*[^>]*value\s*=\s*"")([^""]*)("".*?/>)";
//txtcontent为需要替换的
//txtresult为结果
Regex rgx = new Regex(strPatter, RegexOptions.Multiline);
this.txtresult.Text = rgx.Replace(txtcontent.Text, new MatchEvaluator(Encode));
}
//调用委托
private string Encode(Match m)
{
string strValue1 = m.Groups[1].Value;
string strValue2 = m.Groups[2].Value;
string strValue3 = m.Groups[3].Value;
return strValue1 + EncodeUniCode(strValue2) + strValue3;
}
//对中文编码成Unicode
private string EncodeUniCode(string input)
{
Encoding bigEndianUnicode = Encoding.BigEndianUnicode;
char[] chArray = input.ToCharArray();
string str = "";
foreach (char ch in chArray)
{
if (ch.Equals('\r') || ch.Equals('\n'))
{
str = str + ch;
}
else
{
byte[] bytes = bigEndianUnicode.GetBytes(new char[] { ch });
str = (str + @"\u") + string.Format("{0:X2}", bytes[0]) + string.Format("{0:X2}", bytes[1]);
}
}
return str;
}
{
string strPatter = @"(<input\s*[^>]*value\s*=\s*"")([^""]*)("".*?/>)";
//txtcontent为需要替换的
//txtresult为结果
Regex rgx = new Regex(strPatter, RegexOptions.Multiline);
this.txtresult.Text = rgx.Replace(txtcontent.Text, new MatchEvaluator(Encode));
}
//调用委托
private string Encode(Match m)
{
string strValue1 = m.Groups[1].Value;
string strValue2 = m.Groups[2].Value;
string strValue3 = m.Groups[3].Value;
return strValue1 + EncodeUniCode(strValue2) + strValue3;
}
//对中文编码成Unicode
private string EncodeUniCode(string input)
{
Encoding bigEndianUnicode = Encoding.BigEndianUnicode;
char[] chArray = input.ToCharArray();
string str = "";
foreach (char ch in chArray)
{
if (ch.Equals('\r') || ch.Equals('\n'))
{
str = str + ch;
}
else
{
byte[] bytes = bigEndianUnicode.GetBytes(new char[] { ch });
str = (str + @"\u") + string.Format("{0:X2}", bytes[0]) + string.Format("{0:X2}", bytes[1]);
}
}
return str;
}
编码工具下载