WCF异常:文本消息编码和ISO-8859-1编码

Kiq*_*net 6 c# wcf encoding exception-handling iso-8859-1

可能重复:
调用从WCF使用ISO-8859-1编码的Web服务

我正在尝试使用VS 2008,.net 3.5,WCF(环境:Windows XP和VS 2008)来使用外部Web服务(Web服务具有PHP实现).我添加服务引用到Web服务,VS生成WCF代理.

绑定是basicHttpBinding.

我调用Web Service中的方法,使用Proxy,然后我开始收到ProtocolException,我收到以下错误信息:

System.ServiceModel.ProtocolException:内容类型text/xml; charset =响应消息的ISO-8859-1与绑定的内容类型(text/xml; charset = utf-8)不匹配.如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.

响应的前644个字节是:

这很成功

好吧,我需要用iso-8859-1编码来调用服务.

任何有用的示例源代码都可以解决吗?

更新:

WCF中使用的默认编码器仅适用于UTF-8和UTF-16(大端和小端).

如果我在app.config中使用textEncoding ="iso-8859-1",

我收到此错误:

System.ArgumentException:No se admitelacodificacióndetexto'iso-8859-1'usada en el formato de mensaje de texto.Nombredelparámetro:编码.

System.ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding(Encoding encoding)System.ServiceModel.Channels.TextMessageEncodingBindingElement.set_WriteEncoding(Encoding value)System.ServiceModel.BasicHttpBinding.set_TextEncoding(Encoding value)System.ServiceModel.Configuration.BasicHttpBindingElement.OnApplyConfiguration(Binding binding) System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(Binding binding)System.ServiceModel.Description.ConfigLoader.LookupBinding(String bindingSectionName,String configurationName,ContextInformation context)System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint,String configurationName)System. ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName,EndpointAddress address)ctor(String endpointConfigurationName,EndpointAddress remoteAddress)CreateSimplexFactory()CreateChannelFactory()CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)InitializeChannelFactoryRef()ctor()IntegracionEasyVista.ServiceEasyVista.WebServicePortTypeClient..ctor()in

更新:

调用从WCF使用ISO-8859-1编码的Web服务

调用从WCF使用ISO-8859-1编码的Web服务

这个MSDN页面(http://msdn.microsoft.com/en-us/library/ms751486(v=VS.90).aspx)展示了如何创建一个"CustomTextEncoder",它可以支持超过utf-8,utf- 16和unicode编码.它包含完整的示例源代码,对我来说非常有用.

我使用CustomTextMessageEncodingElement,但是我收到错误:

内容类型text/xml; charset = ISO-8859-1的响应消息与绑定的内容类型不匹配(text/xml; charset = iso-8859-1).如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.响应的前1024个字节是:**

从Samples MSDN的代码中,我修改了CustomTextMessageEncoder类的构造函数:

public CustomTextMessageEncoder(CustomTextMessageEncoderFactory factory)
{
    writerSettings = new XmlWriterSettings();
    writerSettings.Encoding = Encoding.GetEncoding(factory.CharSet);
    contentType = string.Format("{0};charset={1}", 
    factory.MediaType, this.writerSettings.Encoding.HeaderName);
}
Run Code Online (Sandbox Code Playgroud)

我将"{0}; charset = {1}"替换为"{0}; charset = {1}"(我已经包含空格)

然后,我得到错误:

传出消息的消息版本(Soap11(http://schemas.xmlsoap.org/soap/envelope/)AddressingNone
(http://schemas.microsoft.com/ws/2005/05/addressing/none))不匹配编码器

(Soap12(http://www.w3.org/2003/05/soap-envelope)Addressing10(http://www.w3.org/2005/08/addressing)).
确保使用与消息相同的版本配置绑定.