gpt4 book ai didi

c# - WebService SOAP 编码 UTF-16 而不是 UTF-8

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:15 25 4
gpt4 key购买 nike

是否可以强制 C# .NET 中的 WebService 使用编码 UTF-16 而不是 UTF-8?

我创建了一个接收 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>

任何帮助将不胜感激:-)

最佳答案

您必须配置您的网络服务。转到 Web.config 的全局化部分并制作 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;

关于c# - WebService SOAP 编码 UTF-16 而不是 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32992651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com