如何处理 Web 服务中字符串中的特殊字符?

发布于 2024-07-21 13:35:46 字数 1406 浏览 2 评论 0原文

为了展示 .NET 中的这一基本问题以及出现此问题的原因,我使用一种方法 (EditString) 编写了一个简单的测试 Web 服务,以及一个调用它的消费者控制台应用程序。

它们都是通过文件/新建项目等创建的标准 Web 服务/控制台应用程序,因此我不会列出整个代码 - 仅列出有问题的方法:

Web 方法:

[WebMethod]
public string EditString(string s, bool useSpecial)
{
    return s + (useSpecial ? ((char)19).ToString() : "");
}

[您可以看到它只是返回字符串 s if useSpecial是假的。 如果 useSpecial 为 true,则返回 s + char 19。]

控制台应用程序:

TestService.Service1 service = new SCTestConsumer.TestService.Service1();

string response1 = service.EditString("hello", false);
Console.WriteLine(response1);

string response2 = service.EditString("hello", true); // fails!
Console.WriteLine(response2);

[第二个响应失败,因为该方法返回 hello + 一个特殊字符(为了参数,ascii 代码 19)。]

错误是:

  • 有XML 文档中存在错误 (1, 287)

  • 内部异常:“'',十六进制值 0x13,是无效字符。第 1 行,位置 287 ."

有几点值得一提:

  • 直接浏览到 ASMX 文件(例如 http://localhost:2065/service1.asmx)并通过此运行该方法时,Web 方法本身运行良好(与控制台应用程序中的参数相同) - 即显示带有字符串 hello + char 19 的 XML。

  • 检查其他中的序列化 XML方式显示特殊字符正在被正确编码(服务器端似乎没问题,这是好的)

  • 所以看来客户端有问题 - 即 .NET 生成的代理类代码不处理特殊字符

  • 这是一个更大项目的一部分,其中对象在 Web 方法中传入和传出 - 包含字符串属性 - 这些是正常工作所需的。 即我们正在反/序列化类。

对于解决方法以及如何实施有什么建议吗?

或者我完全错过了一些非常明显的事情!!?

附言。 我没有太多运气让它使用 CDATA 标签(.NET 支持这些开箱即用吗?)。

To show this fundamental issue in .NET and the reason for this question, I have written a simple test web service with one method (EditString), and a consumer console app that calls it.

They are both standard web service/console applications created via File/New Project, etc., so I won't list the whole code - just the methods in question:

Web method:

[WebMethod]
public string EditString(string s, bool useSpecial)
{
    return s + (useSpecial ? ((char)19).ToString() : "");
}

[You can see it simply returns the string s if useSpecial is false. If useSpecial is true, it returns s + char 19.]

Console app:

TestService.Service1 service = new SCTestConsumer.TestService.Service1();

string response1 = service.EditString("hello", false);
Console.WriteLine(response1);

string response2 = service.EditString("hello", true); // fails!
Console.WriteLine(response2);

[The second response fails, because the method returns hello + a special character (ascii code 19 for argument's sake).]

The error is:

  • There is an error in XML document (1, 287)

  • Inner exception: "'', hexadecimal value 0x13, is an invalid character. Line 1, position 287."

A few points worth mentioning:

  • The web method itself WORKS FINE when browsing directly to the ASMX file (e.g. http://localhost:2065/service1.asmx), and running the method through this (with the same parameters as in the console application) - i.e. displays XML with the string hello + char 19.

  • Checking the serialized XML in other ways shows the special character is being encoded properly (the SERVER SIDE seems to be ok which is GOOD)

  • So it seems the CLIENT SIDE has the issue - i.e. the .NET generated proxy class code doesn't handle special characters

  • This is part of a bigger project where objects are passed in and out of the web methods - that contain string attributes - these are what need to work properly. i.e. we're de/serializing classes.

Any suggestions for a workaround and how to implement it?

Or have I completely missed something really obvious!!?

PS. I've not had much luck with getting it to use CDATA tags (does .NET support these out of the box?).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

栀子花开つ 2024-07-28 13:35:47

我正在考虑一些可能对您有帮助的选择。 您可以使用 html 实体而不是 char(19) 来获取路线。 或者正如您所说,您可能想使用 CDATA。

要提出一个干净的解决方案,您可能不想将整个内容放入 CDATA 中。 我不确定为什么您认为 .NET 可能不支持它。 您是在序列化的背景下说的吗?

I am thinking of some options that may help you. You can take the route using html entities instead of char(19). or as you said you may want to use CDATA.

To come up with a clean solution, you may not want to put the whole thing in CDATA. I am not sure why you think it may not be supported in .NET. Are you saying this in the context of serialization?

送舟行 2024-07-28 13:35:46

您将需要使用 byte[] 而不是字符串。

You will need to use byte[] instead of strings.

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