简体   繁体   中英

Problems calling Magento SOAP V2 Service in C#

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 webservice method).

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

  1. C# SOAP - Error in deserializing body of reply message (Magento API)
  2. C#+Magento API V2:The content type text/xml; charset=utf-8,text/xml; charset=UTF-8 of the response message does not match

If got this answer from Rian at Magento Stack Exchange . All credits go to Rian


The solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM