在ASP.NET WebService中接受UTF-8编码字符串

发布于 2025-01-25 02:34:03 字数 1809 浏览 6 评论 0原文

我有一个看起来像这样的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 StreamReaders 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半窗疏影 2025-02-01 02:34:03

最后,事实证明,接受HTTP-Messages中出了问题。我实际上不知道是什么操纵了HTTP-重复,但是我为此找到了解决方法。 eventhough提琴手向我展示了我的application_beginrequest request> requestcontext.httpcontext.httpcontext.request.request.contenttype /code>只是text/xml,它导致在ASMX Serializer中编码默认值(ASCII)的后备。我已将以下代码添加到application_beginrequest处理程序,现在一切正常。

if (Request.RequestContext.HttpContext.Request.ContentType.Equals("text/xml"))
{
    Request.RequestContext.HttpContext.Request.ContentType = "text/xml; charset=UTF-8";
}

感谢您的帮助!

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 my Application_BeginRequest the Request.RequestContext.HttpContext.Request.ContentType was just text/xml, which lead to a fallback to default (ASCII) encoding within the ASMX serializer. I've added the following code to the Application_BeginRequest handler and everything works for now.

if (Request.RequestContext.HttpContext.Request.ContentType.Equals("text/xml"))
{
    Request.RequestContext.HttpContext.Request.ContentType = "text/xml; charset=UTF-8";
}

Thanks for your help!

酒绊 2025-02-01 02:34:03

尝试以下操作: -

  byte[] bytes=Encoding.UTF8.GetBytes(yourString);

注意: -

字符串永远不会包含任何utf-*或其他编码的东西

Try this:-

  byte[] bytes=Encoding.UTF8.GetBytes(yourString);

NOTE:-

Strings never contain anything utf-* or anything else encoded

删除→记忆 2025-02-01 02:34:03

SOAP调用被解码为ASCII某个地方 - 每个Umlauts都是2个字节,设置了高钻头,当解码为ASCII时,它会变成??

因此,类似的事情正在发生:

byte[] bytesSentFromClient = Encoding.UTF8.GetBytes("Ä - Ö - Ü");
string theStringIThenReceiveInMyMethod = Encoding.ASCII.GetString(bytesSentFromClient);
Console.WriteLine(theStringIThenReceiveInMyMethod);
//?? - ?? - ??

确定这一点正在发生,您应该比较stringa ==“ä-ö -Ö -Ö -ü”,而不是在某个地方打印它。

我想您可以从项目范围内搜索“ ASCII”开始,然后如果您发现任何东西,然后从那里工作。

您也可以

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

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:

byte[] bytesSentFromClient = Encoding.UTF8.GetBytes("Ä - Ö - Ü");
string theStringIThenReceiveInMyMethod = Encoding.ASCII.GetString(bytesSentFromClient);
Console.WriteLine(theStringIThenReceiveInMyMethod);
//?? - ?? - ??

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

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

Under the <system.web> tag in Web.config file.

看透却不说透 2025-02-01 02:34:03

我遇到了同样的问题。 ASMX Web服务将我的UTF-8转换为ASCII,或者最好对?您的帖子对我有很大帮助。
我发现的解决方案是将SOAP协议的版本从1.1更改为1.2
我的意思是:

POST /WebService1.asmx HTTP/1.1
Host: www.tempuri.org
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.tempuri.org/HelloWorld"

<?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>
    <HelloWorld xmlns="http://www.tempuri.org/">
        <inputParam>Привет</inputParam>
    </HelloWorld>
  </soap:Body>
</soap:Envelope>

有问题。但是,当我将请求更改为SOAP 1.2:

POST /WebService1.asmx HTTP/1.1
Host: www.tempuri.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <HelloWorld xmlns="http://www.tempuri.org/">
       <inputParam>Привет</inputParam>
    </HelloWorld>
  </soap12:Body>
</soap12:Envelope>

问题已解决。

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:

POST /WebService1.asmx HTTP/1.1
Host: www.tempuri.org
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.tempuri.org/HelloWorld"

<?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>
    <HelloWorld xmlns="http://www.tempuri.org/">
        <inputParam>Привет</inputParam>
    </HelloWorld>
  </soap:Body>
</soap:Envelope>

had the problem. But when I changed my request to SOAP 1.2:

POST /WebService1.asmx HTTP/1.1
Host: www.tempuri.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <HelloWorld xmlns="http://www.tempuri.org/">
       <inputParam>Привет</inputParam>
    </HelloWorld>
  </soap12:Body>
</soap12:Envelope>

The issue was solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文