110,546
社区成员
发帖
与我相关
我的任务
分享
private const string encodingReg = "(?<=(Content\\-Transfer\\-Encoding\\:)).*";
//c 内容
//charset,文字编码方式,utf-8/gb2312等等
//encoding,加密编码方式,包括qp和base64两种
//因为encoding获得的加密编码标志可能不同,所以用了switch.
private string TextAnalyze(string c, string charset, string encoding)
{
switch (encoding.ToLower())
{
case "quoted-printable":
case "qp":
case "q":
try { c = QDecode(Encoding.GetEncoding(charset), c); }
catch (Exception e) { throw new Exception("TextAnalyze(quoted-printabl)" + e.Message); }
break;
case "base64":
case "b":
try
{ c = DecodeBase64(charset, encoding); }
catch (Exception e) { throw new Exception("TextAnalyze(DecodeBase64)" + e.Message); }
break;
}
return c;
}
/// <summary>
/// quoted-printable解码程序.
/// </summary>
/// <param name="encoding">解码目标字符集</param>
/// <param name="data">需要解码的字符串</param>
/// <returns></returns>
private static string QDecode(System.Text.Encoding encoding, string data)
{
MemoryStream strm = new MemoryStream(System.Text.Encoding.Default.GetBytes(data));
int b = strm.ReadByte();
MemoryStream dStrm = new MemoryStream();
while (b > -1)
{
// Hex eg. =E4
if (b == '=')
{
byte[] buf = new byte[2];
strm.Read(buf, 0, 2);
if (!(buf[0] == '\r' && buf[1] == '\n'))
{
int val = int.Parse(System.Text.Encoding.Default.GetString(buf), System.Globalization.NumberStyles.HexNumber);
//int val = int.Parse(System.Text.Encoding.Default.GetString(buf));
byte[] temp = new Byte[] { (byte)val };
dStrm.Write(temp, 0, temp.Length);
}
}
else
{
string encodedChar = encoding.GetString(new byte[] { (byte)b });
byte[] d = System.Text.Encoding.Default.GetBytes(encodedChar);
dStrm.Write(d, 0, d.Length);
}
b = strm.ReadByte();
}
return encoding.GetString(dStrm.ToArray());
}