如何解决WebService参数传递中文的问题
2014-05-08 19:44
369 查看
很多人在调用WebService所提供方法的时候,发现如果当字符串中含有中文,传到WebService端就发现是乱码,这是由于WebService默认的编码是“UTF-8”,当调用端和接收端用不同编码进行转换的时候,就会出现此类问题,如何解决这类问题呢,具体要根据你的环境而定。
如果WebService的代码是你可控的,那么修改起来很简单,只需要把WebService工程中的web.config文件进行修改,即把使用到“utf-8”地方改成“gb2312”就行了。
如果WebService的代码是你不可控的,即你无法去修改WebService工程中的web.config文件,那么做起来就比较费事了,就是你需要亲自把当前字符串转换成“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中出现乱码现象。
如果WebService的代码是你可控的,那么修改起来很简单,只需要把WebService工程中的web.config文件进行修改,即把使用到“utf-8”地方改成“gb2312”就行了。
如果WebService的代码是你不可控的,即你无法去修改WebService工程中的web.config文件,那么做起来就比较费事了,就是你需要亲自把当前字符串转换成“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中出现乱码现象。
相关文章推荐
- 基本数据类型的存储空间
- FZU A-B problem
- #raspberry#基于树梅派的REST Web API构架
- Qt下 QString转char*
- Delphi XE5 android 获取网络状态
- listen(sockRrv,5)中5代表什么
- Javascript获取各种浏览器可见窗口大小
- asp.net+uploadify实现图片上传图片
- Android中的缓存处理及异步加载图片类的封装
- YUV422与RGB互相转换(经验证在IPNC与PC上都可以)
- Android Sensor传感器系统架构初探
- 《UNIX环境高级编程》第二版源码在Ubuntu下的编译
- c#扩展方法简介
- 令人头疼的clientTop、scrollTop、offsetTop
- 在window上安装pandas
- OCP-1Z0-051 第84题 CONCAT,RAPLACE,INITCAP,SUBSTR函数
- 可以限制中文输入的文本框
- getc和fgetc的区别
- FFMPEG详解
- ffmpeg音频编码