简体   繁体   中英

WCF SOAP Service Ignore Content-Type charset

I have a new WCF service that some existing clients need to be able to communicate with.

There are some clients that are incorrectly sending the SOAP request with a Content-Type header of 'text/xml; charset=us-ascii'. At the moment, the clients themselves can't be changed.

When they send a request, they get the error message:

HTTP/1.1 415 Cannot process the message because the content type 'text/xml; charset=us-ascii' was not the expected type 'text/xml; charset=utf-8'

Is there any way to instruct the WCF service to ignore the charset of the Content-Type and assume utf-8?

This may be helpful for you, if not I will delete the answer. I think this can be used in your context, but am not quite sure since there are no other details.

A WebContentTypeMapper can be used to override the content type being sent. This is snipped of the code used in the WCF Sample sets. In this example it is taking in whatever content type that is sent in and mapping it to json, but you could adjust that to your needs.

If you don't have it already you can download the samples from WCF Samples This sample is located in ..WF_WCF_Samples\\WCF\\Extensibility\\Ajax\\WebContentTypeMapper\\CS

using System.ServiceModel.Channels;

namespace Microsoft.Samples.WebContentTypeMapper
{
    public class JsonContentTypeMapper : System.ServiceModel.Channels.WebContentTypeMapper
    {
        public override WebContentFormat GetMessageFormatForContentType(string contentType)
        {
            if (contentType == "text/javascript")
            {
                return WebContentFormat.Json;
            }
            else
            {
                return WebContentFormat.Default;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM