从 WCF 调用使用 ISO-8859-1 编码的 Web 服务 [英] Calling a webservice that uses ISO-8859-1 encoding from WCF

查看:24
本文介绍了从 WCF 调用使用 ISO-8859-1 编码的 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WCF 调用使用以下编码的网络服务:

I am trying to call a webservice using WCF that uses the following encoding:

<?xml version="1.0" encoding="ISO-8859-1" ?> 

我无法更改此网络服务的编码.我生成了一个 wcf 代理,当我尝试调用该代理时,出现以下错误:

I cannot change the encoding for this webservice. I have generated a wcf proxy and when I try and call the proxy, I get the following error:

FailedSystem.ServiceModel.ProtocolException:内容类型 text/xml;响应的字符集=ISO-8859-1留言内容与内容不符绑定类型(text/xml;字符集=utf-8).如果使用自定义编码器,请确保IsContentTypeSupported 方法是正确实施.

FailedSystem.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.

有谁知道我如何使用 wcf 调用非 UTF-8 的网络服务?

Has anyone any idea how I call a webservice that is not UTF-8 using wcf?

推荐答案

很遗憾,您无法在 WCF 中立即执行此操作.

You cannot do this out of the box in WCF - unfortunately.

我遇到了同样的问题,并想出了一个您可以使用的自定义绑定(在配置中或在代码中).它基于 HTTP 传输,如果这对您有帮助,我可以在此处发布自定义配置,或者向您发送指向用于绑定的 C# 代码的链接.

I faced the same issue, and came up with a custom binding (either in config or in code) that you can use. It's based on HTTP transport, and if that's of help to you, I could post the custom config here, or send you a link to the C# code for the binding.

此处的 MSDN 页面展示了如何创建CustomTextEncoder"它可以支持超过 utf-8、utf-16 和 unicode 编码.它包含完整的示例源代码,对我开始工作非常有用.

This MSDN page here shows how to create a "CustomTextEncoder" which can support more than utf-8, utf-16 and unicode encodings. It includes full sample source code and was very useful for me to get things going.

这个 此处的博文 展示了在尝试使自定义文本编码器正常工作时需要考虑的一些额外事项.

This blog post here shows some additional things to take into account when trying to get that custom text encoder to work properly.

希望有所帮助.

马克

更新:
基于我在上面提到的针对 CustomTextEncoder 的 Microsoft 示例,您可以轻松地使用 ISO-8859-1 文本消息编码创建自定义绑定 - 只需使用此配置片段(假设您已下载并编译并引用了该 Microsoft 示例):

UPDATE:
Based on the Microsoft sample I mentioned above for a CustomTextEncoder, you can easily create a custom binding with a ISO-8859-1 text message encoding - just use this snippet of config (assuming you have downloaded and compiled and referenced that Microsoft sample):

  <system.serviceModel>
    <extensions>
      <bindingElementExtensions>
        <add name="customTextMessageEncoding"
             type="Microsoft.ServiceModel.Samples.CustomTextMessageEncodingElement, 
                   Microsoft.ServiceModel.Samples.CustomTextEncoder"/>
      </bindingElementExtensions>
    </extensions>
    <bindings>
      <customBinding>
        <binding name="ISO8859Binding" >
          <customTextMessageEncoding encoding="ISO-8859-1" />
          <httpTransport />
        </binding>
      </customBinding>
    </bindings>

    <client>
      <endpoint name="Test"
                address="......."
                binding="customBinding"
                bindingConfiguration="ISO8859Binding" 
                contract="IYourContract" />
    </client>
  </system.serviceModel>

您可以在我的 Skydrive 目录 WCF ISO 8859-1 编码.不过要小心——这是基于我当时的要求,例如我的服务需要与所需的 https 对话,以及其他一些有点古怪的设置,因此如果需要,您可能需要调整这些设置或使其在配置中可配置.ISO88591Binding"项目还包含一个 netHttpBinding,它也是一个 Microsoft 提供的示例,我用它来掌握在代码中编写自己的自定义绑定的窍门.

You can find that code, plus a ISO88591Binding that basically wraps this whole config into code, on my Skydrive directory WCF ISO 8859-1 Encoding. Watch out, though - this is based on my requirements I had at the time, e.g. my service I needed to talk to required https, and also some other a bit whacky settings, so you might need to tweak those or make them configurable in config, if needed. That ISO88591Binding" project also contains a netHttpBinding that again is a Microsoft-provided sample which I used to get the hang of writing my own custom binding in code.

在 WCF 中编写自定义绑定绝对是可能的 - 但不是真的适合胆小的人......

Writing a custom binding in WCF is definitely possible - but not really for the faint of heart....

这篇关于从 WCF 调用使用 ISO-8859-1 编码的 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆