代码之家  ›  专栏  ›  技术社区  ›  Kiquenet user385990

WCF异常:文本消息编码和ISO-8859-1编码[重复]

  •  6
  • Kiquenet user385990  · 技术社区  · 15 年前

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

    我正在尝试使用VS2008、.NET3.5、WCF(环境:WindowsXP和VS2008)来使用外部Web服务(Web服务有PHP实现)。我将服务引用添加到Web服务,VS生成WCF代理。

    我使用代理调用Web服务中的方法,然后开始获取ProtocolException,收到以下错误消息:

    System.ServiceModel.ProtocolException:系统服务模型.ProtocolException 内容类型text/xml; 消息与内容不匹配 绑定类型(text/xml; 编码器,确保 IsContentTypeSupported方法是

    响应的前644字节是:

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

    有什么有用的示例源代码可以解决这个问题吗?

    更新:

    WCF中使用的默认编码器仅适用于UTF-8和UTF-16(big和little endian)。

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

    System.ArgumentException:不允许 “iso-8859-1”文本编码 乌萨达市 文本。Nombre del parmetro:编码。

    编码) System.ServiceModel.Channels.TextMessageEncodingBindingElement.set\u WriteEncoding(编码 价值) 价值) System.ServiceModel.Configuration.StandardBindingElement.ApplyConfiguration(绑定 装订) bindingSectionName,字符串 System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(服务终结点 serviceEndpoint,字符串 配置名称) System.ServiceModel.ChannelFactory.ApplyConfiguration(字符串 System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串 地址)ctor(字符串 端点配置名称, EndpointAddress远程地址) 创建通道工厂() CreateChannelFactoryRef(EndpointTrait`1 初始化channelfactoryref()ctor() 集成AsyVista.ServiceEasyVista.WebServicePortTypeClient..ctor() 在里面

    更新:

    从WCF调用使用ISO-8859-1编码的webservice

    此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);
    }
    

    然后,我得到错误:

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

    http://www.w3.org/2003/05/soap-envelope )地址10( http://www.w3.org/2005/08/addressing )).

    2 回复  |  直到 9 年前
        1
  •  4
  •   Community Mohan Dere    9 年前
        2
  •  -3
  •   tomasr    15 年前

    我认为您应该能够通过显式地更改绑定配置来解决这个问题,以设置 textEncoding

    推荐文章