首页 > 解决方案 > ASP.NET SOAP Webservice,异常中的编码问题

问题描述

这是我的问题,我尝试使用以下代码对我的 web 服务的响应进行编码。

public static string ConvertToUTF8(string Cadena)
    {
        string mensajeex = Cadena;
        Encoding utf8 = Encoding.UTF8;
        Encoding unicode = Encoding.Unicode;

        // Convert the string into a byte array.
        byte[] unicodeBytes = unicode.GetBytes(mensajeex);
        // Perform the conversion from one encoding to the other.
        byte[] asciiBytes = Encoding.Convert(unicode, utf8, unicodeBytes);
        // Convert the new byte[] into a char[] and then into a string.
        char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
        utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
        string Utf8string = new string(asciiChars);
        // Display the strings created before and after the conversion.
        Console.WriteLine("Original string: {0}", mensajeex);
        Console.WriteLine("Ascii converted string: {0}", Utf8string);
        return Utf8string;

  }

实际上它有效!但是,当我尝试对字符串进行编码,然后像这样将异常作为 Message 属性传递时

throw new Exception(XMLHelper.ConvertToUTF8(Message));

它给了我错误的回应,例如:

El valor 'R' no es válido seg&#250

有任何想法吗?谢谢

标签: c#asp.netweb-services

解决方案


推荐阅读