在 C# Web 服务中处理 UTF-8 字符串

发布于 2024-09-27 13:23:45 字数 293 浏览 3 评论 0原文

我使用 C# 工具 wsdl.exe 创建了一个简单的 Web 服务客户端。除了一件事之外,它工作得很好。响应中返回的 UTF-8 字符串似乎已转换为 ASCII。使用 SOAPUI,我可以看到 Web 服务返回正常的 UTF-8 编码字符串。但是当我调试收到的响应时,UTF-8 内容似乎已经转换为 ASCII 并且完全是乱码。在我应该看到包含日语字符的字符串的地方,我看到了“?”列表。

  1. 有没有办法将字符串转换回 Unicode,以便在调试器中看到原始响应字符串?
  2. 有没有办法防止服务响应中的字符串出现乱码?

I created a simple web service client using the C# tool wsdl.exe. It works fine except for one thing. It seems that UTF-8 strings returned in response are converted to ASCII. Using SOAPUI I can see normal UTF-8 encoded strings being returned by the web service. But when I debug the response I received the UTF-8 content seems to have already been converted to ASCII and is completely garbled. Where I should see a string containing Japanese characters I'm seeing a list of '?'.

  1. Is there a way to convert back the string to Unicode so that seen in the debugger is the original response string ?
  2. Is there a way to prevent the string from getting garbled in the service response?

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

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

发布评论

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

评论(2

紫竹語嫣☆ 2024-10-04 13:23:45

确保您实际上按照 @Sam 提供的文档对字符串进行编码:

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        // Create a UTF-8 encoding.
        UTF8Encoding utf8 = new UTF8Encoding();

        // A Unicode string with two characters outside an 8-bit code range.
        String unicodeString =
            "This unicode string contains two characters " +
            "with codes outside an 8-bit code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        Byte[] encodedBytes = utf8.GetBytes(unicodeString);
        Console.WriteLine();
        Console.WriteLine("Encoded bytes:");
        foreach (Byte b in encodedBytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();

        // Decode bytes back to string.
        // Notice Pi and Sigma characters are still present.
        String decodedString = utf8.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    }
}

Ensure you are actually encoding the strings as per the documentation @Sam provided:

using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        // Create a UTF-8 encoding.
        UTF8Encoding utf8 = new UTF8Encoding();

        // A Unicode string with two characters outside an 8-bit code range.
        String unicodeString =
            "This unicode string contains two characters " +
            "with codes outside an 8-bit code range, " +
            "Pi (\u03a0) and Sigma (\u03a3).";
        Console.WriteLine("Original string:");
        Console.WriteLine(unicodeString);

        // Encode the string.
        Byte[] encodedBytes = utf8.GetBytes(unicodeString);
        Console.WriteLine();
        Console.WriteLine("Encoded bytes:");
        foreach (Byte b in encodedBytes) {
            Console.Write("[{0}]", b);
        }
        Console.WriteLine();

        // Decode bytes back to string.
        // Notice Pi and Sigma characters are still present.
        String decodedString = utf8.GetString(encodedBytes);
        Console.WriteLine();
        Console.WriteLine("Decoded bytes:");
        Console.WriteLine(decodedString);
    }
}
北座城市 2024-10-04 13:23:45

不要以字符串形式发送数据,而是尝试以字节数组形式发送数据,并在客户端将其转换为相同的编码。

Instead of sending the data as a string, try sending the data as a byte array and convert it to the same encoding on the client side.

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