如何解决WebService参数传递中文的问题
2016-12-30 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517190
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
需要亲自把当前字符串转换成“UTF-8”的编码,具体如下:
///<summary>
/// Change string from default encoding to "UTF-8" encoding
///</summary>
///<param name="sValue"> the string to be changed</param>
///<returns>if changed successfully, return string under "UTF-8" encoding;
///else return null
///</returns>
public static string GetUnicodeString( string sValue )
{
Encoding def = Encoding.Default;
Encoding unicode = Encoding.UTF8;
// Check whether default encoding is same as "UTF-8" encoding
if( def == unicode ) return sValue;
// Check parameter
if( sValue == null || sValue.Length == 0 ) return sValue;
// Convert the string into a byte[].
byte[] defBytes = def.GetBytes( sValue );
// Perform the conversion from one encoding to the other.
byte[] unicodeBytes = Encoding.Convert( def, unicode, defBytes);
char[] uniChars = new char[ unicodeBytes.Length] ;
for( int i = 0; i < unicodeBytes.Length; i++ )
uniChars[i] = (char)(unicodeBytes[i]);
return new string( uniChars );
}
那么调用WebService之前,需要把含有中文的字符串用以上的函数进行转换,然后用转换后的字符串去调用WebService,就可以避免在WebService中出现乱码现象。
///<summary>
/// Change string from default encoding to "UTF-8" encoding
///</summary>
///<param name="sValue"> the string to be changed</param>
///<returns>if changed successfully, return string under "UTF-8" encoding;
///else return null
///</returns>
public static string GetUnicodeString( string sValue )
{
Encoding def = Encoding.Default;
Encoding unicode = Encoding.UTF8;
// Check whether default encoding is same as "UTF-8" encoding
if( def == unicode ) return sValue;
// Check parameter
if( sValue == null || sValue.Length == 0 ) return sValue;
// Convert the string into a byte[].
byte[] defBytes = def.GetBytes( sValue );
// Perform the conversion from one encoding to the other.
byte[] unicodeBytes = Encoding.Convert( def, unicode, defBytes);
char[] uniChars = new char[ unicodeBytes.Length] ;
for( int i = 0; i < unicodeBytes.Length; i++ )
uniChars[i] = (char)(unicodeBytes[i]);
return new string( uniChars );
}
那么调用WebService之前,需要把含有中文的字符串用以上的函数进行转换,然后用转换后的字符串去调用WebService,就可以避免在WebService中出现乱码现象。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询