c# - WCF 异常 : Text Message Encoding and ISO-8859-1 Encoding

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

<分区>

Possible Duplicate:
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 代理。

绑定(bind)是basicHttpBinding。

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

System.ServiceModel.ProtocolException: The content type text/xml; charset=ISO-8859-1 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.

The first 644 bytes of the response were:

This was successful

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

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

更新:

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

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

我收到这个错误:

System.ArgumentException: No se admite la codificación de texto 'iso-8859-1' usada en el formato de mensaje de texto. Nombre del parámetro: encoding.

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 服务

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

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

我使用 CustomTextMessageEncodingElement,但出现错误:

The content type text/xml; charset=ISO-8859-1 of the response message does not match the content type of the binding (text/xml;charset=iso-8859-1). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were:**

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

我将“{0};charset={1}”替换为“{0};charset={1}”(我包含了一个空白)

然后,我得到错误:

The message version of the outgoing message (Soap11 (http://schemas.xmlsoap.org/soap/envelope/) AddressingNone
(http://schemas.microsoft.com/ws/2005/05/addressing/none)) does not match that of the encoder

(Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)).
Make sure the binding is configured with the same version as the message.

最佳答案

关于c# - WCF 异常 : Text Message Encoding and ISO-8859-1 Encoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4123584/

相关文章:

json - WCF 数据服务 5.0 返回 POCO 的解决方法?

java - 如何在java中解码从apache发送的PEM编码的SSL_CLIENT_CERT header 值

c# - 在 C# 中显示带有 "detail"按钮的错误消息框

c# - 如何为动态创建的按钮创建 onClick 事件处理程序

c# - Windows 应用程序通过 HTTPS 正常连接到本地 WCF 服务,Windows 服务失败并显示相同的代码

c# - WCF 发现找到端点但主机是 "localhost"

java - 在 Play Framework 中获取二进制 HTTP GET 参数

c - 使用 libcurl 从特定 URL 接收垃圾数据

c# - 从字符串生成固定大小的字节序列

c# - MS-Project 文件在任务栏上堆积