在ASP.NET WebService中接受UTF-8编码字符串
我有一个看起来像这样的ASP.NET Web服务:
[WebMethod]
public static void DoSomethingWithStrings(string stringA, string stringB)
{
// and so on
}
第三方应用程序应该称呼此WebService。但是,此应用程序将字符串编码为UTF-8和所有Umlauts被“ ??”所取代。我可以查看呼叫,特殊字符的格式很好:
<?xml version="1.0" encoding="utf-8" ?>
<!-- ... -->
<SoapCall>
<DoSomethingWithStrings>
<stringA>Ä - Ö - Ü</stringA>
<stringB>This is a test</stringB>
</DoSomethingWithStrings>
</SoapCall>
这会产生以下输出,当我简单地在WebService方法中打印字符串:
?? - ?? - ??
这是一个测试
如何配置Web服务以接受UTF-8编码字符串?
Update
提琴手还告诉我,HTTP请求的内容类型Charset是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();
}
}
这读取实际的肥皂调用。 StreamReader
s编码设置为UTF-8。肥皂呼叫看起来正确:
<?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编码。
I've got a ASP.NET WebService that looks something like this:
[WebMethod]
public static void DoSomethingWithStrings(string stringA, string stringB)
{
// and so on
}
An third party application should call this webservice. However this application encodes strings as UTF-8 and all umlauts are replaced by '??'. I can view the call and the special characters are formatted well:
<?xml version="1.0" encoding="utf-8" ?>
<!-- ... -->
<SoapCall>
<DoSomethingWithStrings>
<stringA>Ä - Ö - Ü</stringA>
<stringB>This is a test</stringB>
</DoSomethingWithStrings>
</SoapCall>
This produces the following output, when I simply print the strings inside the webservice method:
?? - ?? - ??
This is a test
How can I configure the WebService to accept UTF-8 encoded strings?
Update
Fiddler also tells me that the content-type charset of the http request is UTF-8.
Update 2
I tried to add following code to global.asax
for debugging purposes:
public void Application_BeginRequest(object sender, EventArgs e)
{
using (var reader = new System.IO.StreamReader(Request.InputStream))
{
string str = reader.ReadToEnd();
}
}
This reads the actual SOAP call. The StreamReader
s encoding is set to UTF-8. The SOAP call looks correct:
<?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>
In the web.config
file the globalization settings are set correctly:
<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" culture="de-DE" uiCulture="de-DE" />
So it looks like something that deserializes the SOAP message does not use UTF-8 but ASCII encoding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最后,事实证明,接受HTTP-Messages中出了问题。我实际上不知道是什么操纵了HTTP-重复,但是我为此找到了解决方法。 eventhough提琴手向我展示了我的
application_beginrequest
request> requestcontext.httpcontext.httpcontext.request.request.contenttype /code>只是
text/xml
,它导致在ASMX Serializer中编码默认值(ASCII)的后备。我已将以下代码添加到application_beginrequest
处理程序,现在一切正常。感谢您的帮助!
Finally it turns out that something went wrong within accepting HTTP-Messages. I don't actually know what manipulates the HTTP-Request, but I found a workaround for this. Eventhough Fiddler showed me the correct content type (
text/xml; charset=utf-8
) in myApplication_BeginRequest
theRequest.RequestContext.HttpContext.Request.ContentType
was justtext/xml
, which lead to a fallback to default (ASCII) encoding within the ASMX serializer. I've added the following code to theApplication_BeginRequest
handler and everything works for now.Thanks for your help!
尝试以下操作: -
注意: -
Try this:-
NOTE:-
SOAP调用被解码为ASCII某个地方 - 每个Umlauts都是2个字节,设置了高钻头,当解码为ASCII时,它会变成
??
。因此,类似的事情正在发生:
确定这一点正在发生,您应该比较
stringa ==“ä-ö -Ö -Ö -ü”
,而不是在某个地方打印它。我想您可以从项目范围内搜索“ ASCII”开始,然后如果您发现任何东西,然后从那里工作。
您也可以
在
web&gt;
标记web.config
文件中尝试尝试。The SOAP call is being decoded as ASCII somewhere - each of the umlauts are 2 bytes with high bit being set, which turns into
??
when decoded as ASCII.So, something like this is happening:
To verify this is happening for sure, you should compare
stringA == "Ä - Ö - Ü"
rather than printing it somewhere.I guess you could start by doing a project-wide search for "ASCII" and then work from there if you find anything.
You could also try
Under the
<system.web>
tag inWeb.config
file.我遇到了同样的问题。 ASMX Web服务将我的UTF-8转换为ASCII,或者最好对?您的帖子对我有很大帮助。
我发现的解决方案是将SOAP协议的版本从1.1更改为1.2
我的意思是:
有问题。但是,当我将请求更改为SOAP 1.2:
问题已解决。
I had the same problem. Asmx web service converted my UTF-8 to ASCII or, better to say to ??????. Your post helped me a lot.
The solution I found was to change version of SOAP protocol from 1.1 to 1.2
I mean:
had the problem. But when I changed my request to SOAP 1.2:
The issue was solved.