Connecting to a PHP Web service with WCF in C#

I was connecting to a web service hosted in PHP today when WCF threw the following error:

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 1024 bytes of the response were: …

I did some searching on the internet and it seems to be a known issue.  Here is exactly how I solved it.  I created three new classes which I have included links to download:

CustomTextMessageBindingElement.cs
CustomTextMessageEncoder.cs
CustomTextMessageEncoderFactory.cs

After adding these to your project add the following code to set the custom binding


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

myWebService client = new myWebService();

client.Endpoint.Binding = binding;

Download code here WCF_CodeSample

 

Thanks to these links for the source files and answers

http://msmvps.com/blogs/paulomorgado/archive/2007/04/25/wcf-text-message-encoding-and-iso-8859-1-encoding.aspx

http://msdn.microsoft.com/en-us/library/ms751486.aspx

http://stackoverflow.com/questions/7033442/using-iso-8859-1-encoding-between-wcf-and-oracle-linux

 

 

5 thoughts on “Connecting to a PHP Web service with WCF in C#”

  1. Thank you so much for this – you have really been a great help with solving my problem. Many thanks again for making your efforts available to others with the same problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.