【发布时间】:2012-12-04 14:40:11
【问题描述】:
我有一个看起来像这样的 ASP.NET WebService:
[WebMethod]
public static void DoSomethingWithStrings(string stringA, string stringB)
{
// and so on
}
第三方应用程序应调用此 Web 服务。但是,此应用程序将字符串编码为 UTF-8,并且所有变音符号都替换为“??”。我可以查看通话,特殊字符的格式很好:
<?xml version="1.0" encoding="utf-8" ?>
<!-- ... -->
<SoapCall>
<DoSomethingWithStrings>
<stringA>Ä - Ö - Ü</stringA>
<stringB>This is a test</stringB>
</DoSomethingWithStrings>
</SoapCall>
当我简单地在 webservice 方法中打印字符串时,这会产生以下输出:
?? - ?? - ??
这是一个测试
如何配置 WebService 以接受 UTF-8 编码的字符串?
更新
Fiddler 还告诉我 http 请求的内容类型字符集是 UTF-8。
更新 2
出于调试目的,我尝试将以下代码添加到global.asax:
public void Application_BeginRequest(object sender, EventArgs e)
{
using (var reader = new System.IO.StreamReader(Request.InputStream))
{
string str = reader.ReadToEnd();
}
}
这会读取实际的 SOAP 调用。 StreamReaders 编码设置为 UTF-8。 SOAP 调用看起来正确:
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<DoSomethingWithStrings xmlns="http://www.tempuri.org/">
<stringA>Ä - Ö - Ü</stringA>
<stringB>This is a test!</stringB>
</DoSomethingWithStrings>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
在web.config 文件中正确设置了全球化设置:
<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" culture="de-DE" uiCulture="de-DE" />
所以看起来反序列化 SOAP 消息的东西不使用 UTF-8 而是 ASCII 编码。
【问题讨论】:
-
只是一个技术点,但加密不是正确的形容词。由于 UTF-8 只是一个字符集,编码会更准确。
-
当然你是对的,很抱歉混淆了:)
标签: asp.net web-services unicode encoding utf-8