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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1908030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:40:47  来源:igfitidea点击:

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

.netwcf

提问by dagda1

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

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

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

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:

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

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.

FailedSystem.ServiceModel.ProtocolException: 内容类型 text/xml; 响应消息的 charset=ISO-8859-1 与绑定的内容类型不匹配(text/xml;charset=utf-8)。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法。

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

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

采纳答案by marc_s

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

您不能在 WCF 中开箱即用 - 不幸的是。

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.

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

This MSDN page hereshows 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.

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

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

此处的这篇博文展示了在尝试使该自定义文本编码器正常工作时需要考虑的一些其他事项。

Hope that helps.

希望有帮助。

Marc

马克

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):

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

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

You can find that code, plus a ISO88591Bindingthat 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.

您可以ISO88591Binding在我的 Skydrive 目录WCF ISO 8859-1 Encoding上找到该代码,以及一个基本上将整个配置包装成代码的代码。但是请注意 - 这是基于我当时的要求,例如我的服务需要与所需的 https 通信,以及其他一些有点古怪的设置,因此您可能需要调整这些设置或使它们在配置中可配置, 如果需要的话。ISO88591Binding 项目还包含一个 netHttpBinding,它又是一个 Microsoft 提供的示例,我用它来掌握在代码中编写自己的自定义绑定的窍门。

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

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

回答by Saeb Amini

Another option is using the older .NET Framework 2.0 ASMX WebServices technology which supports iso-8859-1out of the box:

另一种选择是使用较旧的 .NET Framework 2.0 ASMX WebServices 技术,该技术支持iso-8859-1开箱即用:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

And if the service uses Basic HTTP Authentication you can specify it like this:

如果服务使用基本 HTTP 身份验证,您可以像这样指定它:

TheService theService = new TheService();
theService.Credentials = new NetworkCredential("username", "password");

回答by Juan Carlos Velez

In this linkyou can download the files for make the custom binding, and do the following in your code:

在此链接中,您可以下载用于制作自定义绑定的文件,并在您的代码中执行以下操作:

CustomBinding binding = new CustomBinding(
   new CustomTextMessageBindingElement("iso-8859-1", "text/xml", MessageVersion.Soap11),
   new HttpTransportBindingElement());

myWebService client = new myWebService();

client.Endpoint.Binding = binding;

回答by rkawano

If you don't want to deal with tons of downloaded codes and low level implementation you can workaround by using a old style request using HttpWebRequest class, as described here. Now, you will exempt of automatized code and facilities of the Interface and will play with manual Xml parsing.

如果您不想处理大量下载的代码和低级实现,您可以通过使用 HttpWebRequest 类的旧样式请求来解决,如here所述。现在,您将免除接口的自动化代码和工具,并将使用手动 Xml 解析。