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

前端 未结 4 1716
夕颜
夕颜 2020-11-30 06:21

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

 

I cann

相关标签:
4条回答
  • 2020-11-30 07:07

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

    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.

    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.

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

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

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

    0 讨论(0)
  • 2020-11-30 07:07

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

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

    TheService theService = new TheService();
    theService.Credentials = new NetworkCredential("username", "password");
    
    0 讨论(0)
  • 2020-11-30 07:22

    In this link you 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;
    
    0 讨论(0)
  • 2020-11-30 07:23

    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.

    0 讨论(0)
提交回复
热议问题