WebService使用UTF-16编码而非UTF-8

3

在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. ---&gt; 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&amp; abortProcessing) --- End of inner exception stack trace ---</soap:Text>
      </soap:Reason>
      <soap:Detail />
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

非常感谢任何帮助 :-)


对于XML UTF-16,您的响应仅为UTF-8。 - Krsna Kishore
我意识到SOAP是UTF-8编码的,我该如何强制WebService使用UTF-16? - iggyweb
1个回答

3

您需要配置您的网络服务。前往Web.config的全球化部分,将UTF-8更改为UTF-16。应将globalization标签的requestEncoding和responseEncoding属性设置为UTF-16。

<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>

必须被转换为

<globalization requestEncoding="utf-16" responseEncoding="utf-16"/>

这个更改将允许网络服务接受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;

这个能否通过单独的WebService实现,而不是通过Web.config文件?我们正在托管其他WebServices,我认为使用Web.config方法会影响到所有其他托管的WebServices。 - iggyweb
虽然以前从未尝试过,但应该可以完成。我已经编辑了我的答案,你可以使用那段代码来为单个 Web 服务执行此操作。 - Gagan Jaura
谢谢,我已将您的代码添加到我的WebService开头,但它仍然显示 <?xml version="1.0" encoding="utf-8"?>。有什么想法吗? - iggyweb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接