我正在尝试使用 VS 2008、.net 3.5、WCF(环境:Windows XP 和 VS 2008)来使用外部 Web 服务(该 Web 服务具有 PHP 实现)。我将服务引用添加到 Web 服务,VS 生成 WCF 代理。
绑定是basicHttpBinding。
我使用代理调用 Web 服务中的方法,然后我开始收到 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 admissione la codificación de texto 'iso-8859-1' usada en el formato de mensaje de texto。Nombre del parametro:编码。
System.ServiceModel.Channels.TextEncoderDefaults.ValidateEncoding(编码编码) System.ServiceModel.Channels.TextMessageEncodingBindingElement.set_WriteEncoding(编码值) System.ServiceModel.BasicHttpBinding.set_TextEncoding(编码值) System.ServiceModel.Configuration.BasicHttpBindingElement.OnApplyConfiguration(绑定绑定) System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(绑定绑定) System.ServiceModel.Description.ConfigLoader.LookupBinding(字符串 bindingSectionName,字符串 configurationName,ContextInformation 上下文) System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint,String configurationName)系统。 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 个字节是:**
从示例 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);
}
我将“{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))。
确保绑定配置为与消息相同的版本。