webservice 特殊字符处理
最近做了一个webservice应用,发现传输过程中,只要有特殊这符,就会导致出错
于是上网搜了一些方法,先把源WebService输出的内容进行特殊字符转换十六进制,接到收后再进行返转换
/// <summary>
///StringExtenstion 的摘要说明
/// </summary>
public static class StringExtenstion
{
/// <summary>
/// 把一个字符串中的 低序位 ASCII 字符 替换成 &#x 字符
/// 转换 ASCII 0 - 8 -> � -
/// 转换 ASCII 11 - 12 -> -
/// 转换 ASCII 14 - 31 -> -
/// </summary>
/// <param name="tmp"></param>
/// <returns></returns>
public static string ReplaceLowOrderASCIICharacters(this string tmp)
{
StringBuilder info = new StringBuilder();
foreach (char cc in tmp)
{
int ss = (int)cc;
if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))
info.AppendFormat("&#x{0:X};", ss);
else info.Append(cc);
}
return info.ToString();
}
/// <summary>
/// 把一个字符串中的下列字符替换成 低序位 ASCII 字符
/// 转换 � - -> ASCII 0 - 8
/// 转换 - -> ASCII 11 - 12
/// 转换 - -> ASCII 14 - 31
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetLowOrderASCIICharacters(this string input)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
int pos, startIndex = 0, len = input.Length;
if (len <= 4) return input;
StringBuilder result = new StringBuilder();
while ((pos = input.IndexOf("&#x", startIndex)) >= 0)
{
bool needReplace = false;
string rOldV = string.Empty, rNewV = string.Empty;
int le = (len - pos < 6) ? len - pos : 6;
int p = input.IndexOf(";", pos, le);
if (p >= 0)
{
rOldV = input.Substring(pos, p - pos + 1);
// 计算 对应的低位字符
short ss;
if (short.TryParse(rOldV.Substring(3, p - pos - 3), NumberStyles.AllowHexSpecifier, null, out ss))
{
if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))
{
needReplace = true;
rNewV = Convert.ToChar(ss).ToString();
}
}
pos = p + 1;
}
else pos += le;
string part = input.Substring(startIndex, pos - startIndex);
if (needReplace) result.Append(part.Replace(rOldV, rNewV));
else result.Append(part);
startIndex = pos;
}
result.Append(input.Substring(startIndex));
return result.ToString();
///StringExtenstion 的摘要说明
/// </summary>
public static class StringExtenstion
{
/// <summary>
/// 把一个字符串中的 低序位 ASCII 字符 替换成 &#x 字符
/// 转换 ASCII 0 - 8 -> � -
/// 转换 ASCII 11 - 12 -> -
/// 转换 ASCII 14 - 31 -> -
/// </summary>
/// <param name="tmp"></param>
/// <returns></returns>
public static string ReplaceLowOrderASCIICharacters(this string tmp)
{
StringBuilder info = new StringBuilder();
foreach (char cc in tmp)
{
int ss = (int)cc;
if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))
info.AppendFormat("&#x{0:X};", ss);
else info.Append(cc);
}
return info.ToString();
}
/// <summary>
/// 把一个字符串中的下列字符替换成 低序位 ASCII 字符
/// 转换 � - -> ASCII 0 - 8
/// 转换 - -> ASCII 11 - 12
/// 转换 - -> ASCII 14 - 31
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string GetLowOrderASCIICharacters(this string input)
{
if (string.IsNullOrEmpty(input)) return string.Empty;
int pos, startIndex = 0, len = input.Length;
if (len <= 4) return input;
StringBuilder result = new StringBuilder();
while ((pos = input.IndexOf("&#x", startIndex)) >= 0)
{
bool needReplace = false;
string rOldV = string.Empty, rNewV = string.Empty;
int le = (len - pos < 6) ? len - pos : 6;
int p = input.IndexOf(";", pos, le);
if (p >= 0)
{
rOldV = input.Substring(pos, p - pos + 1);
// 计算 对应的低位字符
short ss;
if (short.TryParse(rOldV.Substring(3, p - pos - 3), NumberStyles.AllowHexSpecifier, null, out ss))
{
if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))
{
needReplace = true;
rNewV = Convert.ToChar(ss).ToString();
}
}
pos = p + 1;
}
else pos += le;
string part = input.Substring(startIndex, pos - startIndex);
if (needReplace) result.Append(part.Replace(rOldV, rNewV));
else result.Append(part);
startIndex = pos;
}
result.Append(input.Substring(startIndex));
return result.ToString();
}