在C#中调用Magento SOAP V2服务的问题#

问题描述:

我在本地机器上使用IIS 7.5和PHP 5.6(测试)托管一个magento webshop。这家商店工作得很好,但现在我想用Visual Studio 2013创建一个单独的应用程序。这些是我采取的步骤:在C#中调用Magento SOAP V2服务的问题#

  1. 我已经添加了域名“www.domain.com .local“我的主机文件指向我的本地主机(www.domain.com.local - > 127.0.0.1)
  2. 我在我的IIS上创建了一个新的网站,并添加了一个新的绑定(www.domain.com。地方 - 参见图1)
  3. 我添加WINCACHE扩展与PHP管理和PHP版本设置为5.6
  4. 在Magento的后端启用WS-I遵从性(系统>配置> Magento的核心API)
  5. 创建SOAP角色和用户(资源访问=所有)
  6. 打开Visual Studio 2013,并创建一个新的控制台应用程序
  7. 添加新的服务引用(http://www.domain.com.local/index.php/api/v2_soap/?wsdl
  8. 尝试登录 - 这不是工作像它应该是

这里是我的一段代码:

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(); 

     } 
    } 
} 

这是我得到的错误:

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

因此,大家可以看到,我得到我的sessionId但持续看到这个错误。我也用小提琴来调查并得到正确的答案:HTTP 200 OK。有人知道问题可能是什么吗?它与IIS有关吗?本地主机相关?

(当我将url添加为web引用时,它工作得很好 - 旧的webservice方法)。

相关信息我已阅读并尝试(没有成功):

  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

如果得到了日安这个答案在Magento Stack Exchange。所有学分去Rian


解决办法:

您遇到的问题是,.NET/C#是有问题的解析内容类型的Magento与它发送沿的响应。 SOAP以恰当的格式正确地接收正确的东西非常挑剔。再加上PHP的协议执行不力,你会感到很有趣。

我正在看一个Magento 1。9以下信息:

一些挖后,我发现,对于SOAP调用的头在app/code/core/Mage/Api/Model/Server/V2/Adapter/Soap.php设定上线52

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

注意,Content-Type头的text/xml; charset=utf-8所需的字符集相匹配。调整行52到:

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

告诉Magento如果已经设置,强制覆盖该标头。

请确保使用它的app/code/local/Mage/...的完整路径复制文件以避免覆盖核心文件。当你想在某个时候升级Magento时,你会感谢我。

另外,请务必仔细看看,该文件中有两个setHeader()调用。

最后,还有一个WS-I兼容的SOAP适配器可用,同样的修复适用于该文件。你可以在app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php找到它。