WebService SOAP编码UTF-16而不是UTF-8
本文关键字:UTF-8 UTF-16 SOAP 编码 WebService | 更新日期: 2023-09-27 18:16:40
是否可以强制c# .NET中的WebService使用编码UTF-16而不是UTF-8?
我已经创建了一个简单的WebService来接收XML文档。
WebService.asmx
public class WebApplication1 : WebService
{
[WebMethod(Description = "Test")]
public string Test(XmlDocument xmlDocument)
{
return "Test";
}
}
POST /WebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Test"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Test xmlns="http://tempuri.org/">
<xmlDocument>xml</xmlDocument>
</Test>
</soap:Body>
</soap:Envelope>
如果我尝试传递一个UTF-16编码的XML文档,我会得到以下错误:
XML Content:
<?xml version="1.0" encoding="utf-16"?> <Affiliate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" leadinnumber="ABCDABCD" xmlns:xsd="http://www.w3.org/2001/XMLSchema" mediaid="30000">
<Details>
<Amount>5000</Amount>
<FirstName>Delivery</FirstName>
<Surname>Testing</Surname>
<Tel>02034056978</Tel>
<Email>adfsdfsdfsd@live.co.uk</Email>
</Details>
</Affiliate>
Post Response
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>soap:Receiver</soap:Value>
</soap:Code>
<soap:Reason>
<soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: There is no Unicode byte order mark. Cannot switch to Unicode. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res) at System.Xml.XmlTextReaderImpl.CheckEncoding(String newEncodingName) at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl) at System.Xml.XmlTextReaderImpl.Read() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() at System.Xml.XmlReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) --- End of inner exception stack trace ---</soap:Text>
</soap:Reason>
<soap:Detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>
任何帮助将不胜感激:-)
你必须配置你的web服务。去Web的全球化部分。配置UTF-16而不是UTF-8。全球化标签的requestEncoding和responseEncoding属性应该设置为UTF-16。
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
必须转换为
<globalization requestEncoding="utf-16" responseEncoding="utf-16"/>
此更改将允许web服务接受请求并以UTF-16发送响应。
编辑
通过代码手动设置编码,可以使用以下代码:
// Set the path of the config file.
string configPath = "";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
GlobalizationSection configSection = (GlobalizationSection)config.GetSection("system.web/globalization");
configSection.RequestEncoding = Encoding.Unicode;
configSection.ResponseEncoding = Encoding.Unicode;