WebService SOAP Encoding UTF-16 instead of UTF-8

13,876

You have to configure your web service. Go to globalization section of Web.config and make UTF-16 instead of UTF-8. The requestEncoding and responseEncoding attributes of the globalization tag should be set to UTF-16.

<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>

must be converted to

<globalization requestEncoding="utf-16" responseEncoding="utf-16"/>

This change will allow the web service to accept request and send response in UTF-16.

EDIT

For setting the encoding manually through code you can use following code:

        // Set the path of the config file.
        string configPath = "";            
        Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
        GlobalizationSection configSection = (GlobalizationSection)config.GetSection("system.web/globalization");            
        configSection.RequestEncoding = Encoding.Unicode;
        configSection.ResponseEncoding = Encoding.Unicode;
Share:
13,876
iggyweb
Author by

iggyweb

Updated on June 04, 2022

Comments

  • iggyweb
    iggyweb almost 2 years

    Is it possible to force a WebService in C# .NET to use encoding UTF-16 instead of UTF-8?

    I have created a simple WebService which receives a XML Document.

    WebService.asmx

    public class WebApplication1 : WebService
    {
        [WebMethod(Description = "Test")]
        public string Test(XmlDocument xmlDocument)
        {
            return "Test";
        }
    }
    

    Output

    POST /WebService.asmx HTTP/1.1
    Host: localhost
    Content-Type: text/xml; charset=utf-8
    Content-Length: length
    SOAPAction: "http://tempuri.org/Test"
    
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <Test xmlns="http://tempuri.org/">
          <xmlDocument>xml</xmlDocument>
        </Test>
      </soap:Body>
    </soap:Envelope>
    

    If I try an pass an XML Document that is UTF-16 encoded, I get the following error:

    XML Content:
    <?xml version="1.0" encoding="utf-16"?> <Affiliate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" leadinnumber="ABCDABCD" xmlns:xsd="http://www.w3.org/2001/XMLSchema" mediaid="30000">
      <Details>
        <Amount>5000</Amount>
        <FirstName>Delivery</FirstName>
        <Surname>Testing</Surname>
        <Tel>02034056978</Tel>
        <Email>[email protected]</Email>
      </Details>
    </Affiliate>
    
    Post Response
    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
        <soap:Fault>
          <soap:Code>
            <soap:Value>soap:Receiver</soap:Value>
          </soap:Code>
          <soap:Reason>
            <soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: There is no Unicode byte order mark. Cannot switch to Unicode. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res) at System.Xml.XmlTextReaderImpl.CheckEncoding(String newEncodingName) at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl) at System.Xml.XmlTextReaderImpl.Read() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() at System.Xml.XmlReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing) --- End of inner exception stack trace ---</soap:Text>
          </soap:Reason>
          <soap:Detail />
        </soap:Fault>
      </soap:Body>
    </soap:Envelope>
    

    Any help would be much appreciated :-)