user473098
user473098

Reputation: 31

Handling UTF-8 strings in C# web service

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?

Upvotes: 3

Views: 4760

Answers (2)

Zesty
Zesty

Reputation: 2981

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.

Upvotes: 0

gotnull
gotnull

Reputation: 27214

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);
    }
}

Upvotes: 1

Related Questions