【问题标题】:WCF Exception: Text Message Encoding and ISO-8859-1 Encoding [duplicate]WCF 异常:文本消息编码和 ISO-8859-1 编码 [重复]
【发布时间】:2011-05-06 15:11:26
【问题描述】:

可能重复:
Calling a webservice that uses ISO-8859-1 encoding from WCF

我正在尝试使用 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 的响应 消息与内容不符 绑定类型(文本/xml; 字符集=utf-8)。如果使用自定义 编码器,请确保 IsContentTypeSupported 方法是 正确实施。

响应的前 644 个字节是:

这很成功

嗯,我需要调用 iso-8859-1 编码的服务。

任何有用的示例源代码来解决它?

更新:

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

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

我收到此错误:

System.ArgumentException: 不承认 la codificación de texto 'iso-8859-1' usada en el formato de mensaje de 文本。 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(字符串 绑定部分名称,字符串 配置名称,上下文信息 语境) System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint 服务端点,字符串 配置名称) System.ServiceModel.ChannelFactory.ApplyConfiguration(字符串 配置名称) System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串 配置名称、端点地址 地址)ctor(字符串 端点配置名称, 端点地址远程地址) 创建SimplexFactory() 创建通道工厂() CreateChannelFactoryRef(EndpointTrait`1 端点特征) InitializeChannelFactoryRef() ctor() IntegracionEasyVista.ServiceEasyVista.WebServicePortTypeClient..ctor() 在

更新:

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

Calling a webservice that uses ISO-8859-1 encoding from WCF

这个 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 (@ 987654327@))。
确保绑定配置为与消息相同的版本。

【问题讨论】:

    标签: c# wcf encoding exception-handling iso-8859-1


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我认为您应该能够通过显式更改绑定配置以将textEncoding 属性设置为“iso-8859-1”而不是默认值“utf-8”来解决此问题。

      【讨论】:

      • 在 TextEncoderDefaults.ValidateEncoding(编码编码)中对我来说失败了。 System.ArgumentException: 不支持 textencoding 'iso-8859-1'
      • 你是对的;我之前没有测试过。 WCF 中可能存在一些愚蠢的限制。
      • 最好删除这个答案。
      • 更改 CustomTextMessageBindingElement 构造函数的实现为我排序 public CustomTextMessageBindingElement() : this("ISO-8859-1") { }
      • 我使用 UTF-8,但我仍然收到此错误。我在我的代码中搜索了 8859 并且 VS 没有找到它。又一个误导性的错误信息!
      猜你喜欢
      • 2011-02-12
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 2010-12-27
      相关资源
      最近更新 更多