4

I'm hosting a Magento webshop on my local machine using IIS 7.5 and PHP 5.6 (testing). The shop is working just fine, but now I want to create a separate application using visual studio 2013. These are the steps that I've taken:

  1. I've added the domain name "www.domain.com.local" to my hosts file directing to my localhost (www.domain.com.local -> 127.0.0.1)
  2. I've created a new website on my IIS and added a new binding (www.domain.com.local - see 1)
  3. I've added WinCache extension with the PHP Manager and set the PHP version to 5.6
  4. Enable WS-I Compliance in the magento backend (System > Configuration > Magento Core Api)
  5. Create a SOAP Role and User (Resource Access = All)
  6. Open visual studio 2013 and create a new Console Application
  7. Adding a new Service Reference (http://www.domain.com.local/index.php/api/v2_soap/?wsdl)
  8. Trying to login - This is not working like it should be

Here is my piece of code:

class Program
{
    static void Main(string[] args)
    {
        MainAsync(args).Wait();
    }

    static async Task MainAsync(string[] args)
    {

        using (var proxy = new Mage_Api_Model_Server_Wsi_HandlerPortTypeClient())
        {

            try
            {
                var loginResponse = await proxy.loginAsync("soap-admin", "xxxxxxxxx"); // api key
                var sessionId = loginResponse.result;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();

        }
    }
}

And this is the error I'm getting:

The content type text/xml; charset=utf-8,text/xml; charset=utf-8 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 297 bytes of the response were: '<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento">
<SOAP-ENV:Body>
<ns1:loginResponseParam>
<result>13fa067676759c3ce8ddd61c386b6d5c</result>
</ns1:loginResponseParam>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'.

So as you can see, I'm getting my sessionId but keep getting this error. I've also used fiddler to investigate and getting a correct response: HTTP 200 OK. Does someone knows what the problem could be? Is it IIS related? localhost related?

(When I add the URL as web reference it works just fine - old web-service method).

Related topics I've read and tried (without success):

  1. https://stackoverflow.com/questions/7996931/c-sharp-soap-error-in-deserializing-body-of-reply-message-magento-api?rq=1
  2. https://stackoverflow.com/questions/20591862/cmagento-api-v2the-content-type-text-xml-charset-utf-8-text-xml-charset-utf?rq=1
2
  • Have you assign permission to that user?
    – Keyul Shah
    Nov 5, 2014 at 8:05
  • Yes I did, I've created a test user and role with full rights and privileges.
    – Nesse
    Nov 5, 2014 at 8:19

1 Answer 1

7

The problem you're experiencing is that .NET/C# is having trouble parsing the content type Magento is sending along with it's response. SOAP is notoriously finicky about receiving just the right stuff in just the right format. Couple that with PHP's rather poor implementation of the protocol and you're in for a lot of fun.

I'm looking at a Magento 1.9 for the following information:

After some digging I found that the header for the SOAP calls are set in app/code/core/Mage/Api/Model/Server/V2/Adapter/Soap.php on line 52.

51. ->clearHeaders()
52. ->setHeader('Content-Type','text/xml; charset='.$apiConfigCharset)
53. ->setBod...

Note that that Content-Type header matches your text/xml; charset=utf-8 desired charset. Adjusting line 52 to:

52. ->setHeader('Content-Type','text/xml; charset='.$apiConfigCharset, true)

tells Magento to force overwriting that header if it's already set.

Make sure to make a copy of the file with it's full path to the app/code/local/Mage/... to avoid overwriting core files. You'll thank me when you want to upgrade Magento at some point.

Also, make sure to look carefully, there's two setHeader() calls in that file.

And finally, there's also a WS-I compliant SOAP adapter available, the same fix applies to that file. You can find it in app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php.

2
  • Thx Rian. Your solution solved my problem. I've copied the files to the local directory and edited them like you've said. Great Help.
    – Nesse
    Nov 12, 2014 at 9:19
  • In case WS-I compliance is turned on, the SOAP calls are handled by the run() method in app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php. The line to change is 62 and most likely also 106 (of course in your local copy). Jan 6, 2016 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.