Skip to main content
 首页 » 编程设计

c#之Mono:Webservice 在 XML 中添加 UTF-8 BOM

2024年01月11日29飞鱼

我在 Visual Studio 2008 中完成了一个项目,并在 Linux 上使用 Monodevelop 2.4.2 和 mono 2.10.2 对其进行了重建。

单声道版本生成的输出 SOAP 包含服务器无法处理的 UTF-8 BOM (ef bb bf) header ,生成以下异常:

"Couldn't create SOAP message due to exception: XML reader error: com.sun.xml.stream.XMLStreamException2: ParseError at [row,col]:[1,1] 
Message: Content is not allowed in prolog." 

如何指示单声道自动生成的网络服务客户端不包含此 BOM?

Wireshark 嗅探:

000000B5  ef bb bf 3c 3f 78 6d 6c  20 76 65 72 73 69 6f 6e ...<?xml  version 
000000C5  3d 22 31 2e 30 22 20 65  6e 63 6f 64 69 6e 67 3d ="1.0" e ncoding= 
000000D5  22 75 74 66 2d 38 22 3f  3e 3c 73 3a 45 6e 76 65 "utf-8"? ><s:Enve 

请您参考如下方法:

如果你使用basicHttpBinding创建客户端,你应该设置Encoding without BOM

ClientBaseImp client = new ClientBaseImp(); 
((BasicHttpBinding)client.Endpoint.Binding).TextEncoding  
        = new UTF8Encoding(false); 

最后一行 new UTF8Encoding(false) 删除 BOM。