3

I'm struggling with the encoding of one of my string.

On a Mail Sending WS, I'm receiving a bad string containing "�" instead of "é" (that's what I'm seeing in the Debug Mode of Visual Studio at least). The character comes from some JSON that is deserialized when entering the WS into my DTO.

Changing the Content-Type of the JSON is not solving the thing. So I thought I'll change the encoding of my string by myself, because the JSON encoding thing seems like a VS deserialization issue (I started a thread here if one of you guys want to take a look at it).

I tried :

Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding defaultEncoding = Encoding.Default;
byte[] bytes = defaultEncoding.GetBytes(messedUpString);
byte[] isoBytes = Encoding.Convert(defaultEncoding, iso, bytes);
cleanString = iso.GetString(isoBytes);

Or :

byte[] bytes = Encoding.Default.GetBytes(messedUpString);
cleanString = Encoding.UTF8.GetString(bytes);

And it's not really effective... I get rid of the "�" char, which is the nice part, but I'm receiving in the cleanString "?" instead of the expected "é", and this in not really nice, or at least, the expected behavior.

3
  • 8
    If your data is already a string it's too late to fix it. You need to use the correct encoding when you first convert your data from bytes into a string. Commented Aug 10, 2015 at 13:01
  • @MattiVirkkunen I can't change this. This is a MVC2 method controller, and the Deserialisation feature is part of the Framework.This means I can't do anything to solve this issue? Commented Aug 10, 2015 at 13:20
  • 2
    Well, you have to change it as there is no other way to fix it. You can configure the input encoding in ASP.NET, and it should be able to detect it from the headers as well. As a last resort you can always read the request body as binary and decode it yourself. Commented Aug 10, 2015 at 13:22

1 Answer 1

1

In fact, every thing was fine in my application.

I used SOAPUI to test, and this was my error. I downloaded some rest plugin for my browser, try from there, and everything worked.

Thanks for the help though @MattiVirkkunen

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.