简体   繁体   English

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

[英]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). 我正在使用IIS 7.5和PHP 5.6(测试)在本地计算机上托管一个magento网上商店。 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: 商店工作得很好,但现在我想使用Visual Studio 2013创建一个单独的应用程序。这些是我已经采取的步骤:

  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) 我已将域名“ www.domain.com.local”添加到指向我的本地主机的主机文件中(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) 我在IIS上创建了一个新网站,并添加了新的绑定(www.domain.com.local-参见1)
  3. I've added WinCache extension with the PHP Manager and set the PHP version to 5.6 我在PHP Manager中添加了WinCache扩展,并将PHP版本设置为5.6。
  4. Enable WS-I Compliance in the magento backend (System > Configuration > Magento Core Api) 在magento后端中启用WS-I遵从性(系统>配置> Magento Core Api)
  5. Create a SOAP Role and User (Resource Access = All) 创建一个SOAP角色和用户(资源访问权限=全部)
  6. Open visual studio 2013 and create a new Console Application 打开Visual Studio 2013并创建一个新的控制台应用程序
  7. Adding a new Service Reference ( http://www.domain.com.local/index.php/api/v2_soap/?wsdl ) 添加新的服务参考( 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. 如您所见,我正在获取我的sessionId,但始终收到此错误。 I've also used fiddler to investigate and getting a correct response: HTTP 200 OK. 我还使用提琴手进行调查并获得正确的响应:HTTP 200 OK。 Does someone knows what the problem could be? 有人知道可能是什么问题吗? Is it IIS related? 与IIS相关吗? localhost related? 本地主机相关吗?

(When I add the url as web reference it works just fine - old webservice method). (当我将URL添加为Web引用时,它可以正常工作-旧的webservice方法)。

Related topics I've read and tried (without success): 我已阅读并尝试过的相关主题(没有成功):

  1. C# SOAP - Error in deserializing body of reply message (Magento API) C#SOAP-反序列化回复消息正文时出错(Magento API)
  2. C#+Magento API V2:The content type text/xml; C#+ Magento API V2:内容类型为text / xml; charset=utf-8,text/xml; 字符集= UTF-8,文本/ XML; charset=UTF-8 of the response message does not match 响应消息的charset = UTF-8不匹配

If got this answer from Rian at Magento Stack Exchange . 如果从Magento Stack Exchange的 Rian那里得到了这个答案。 All credits go to Rian 所有学分归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. 您遇到的问题是.NET / C#无法解析Magento随其响应发送的内容类型。 SOAP is notoriously finicky about receiving just the right stuff in just the right format. 众所周知,SOAP以正确的格式接收正确的内容是挑剔的。 Couple that with PHP's rather poor implementation of the protocol and you're in for a lot of fun. 将其与PHP协议的较差实现相结合,您将获得很多乐趣。

I'm looking at a Magento 1.9 for the following information: 我正在寻找Magento 1.9,以获取以下信息:

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. 经过一番挖掘后,我发现在第52行的app/code/core/Mage/Api/Model/Server/V2/Adapter/Soap.php中设置了SOAP调用的标头。

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 请注意,Content-Type标头与您的text/xml; charset=utf-8相匹配text/xml; charset=utf-8 text/xml; charset=utf-8 desired charset. text/xml; charset=utf-8所需的字符集。 Adjusting line 52 to: 将第52行调整为:

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

tells Magento to force overwriting that header if it's already set. 告诉Magento强制覆盖已设置的标头。

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. 确保使用该文件的完整路径复制一份文件到app/code/local/Mage/...以避免覆盖核心文件。 You'll thank me when you want to upgrade Magento at some point. 当您想升级Magento时,会感谢我。

Also, make sure to look carefully, there's two setHeader() calls in that file. 另外,请确保仔细查看,该文件中有两个setHeader()调用。

And finally, there's also a WS-I compliant SOAP adapter available, the same fix applies to that file. 最后,还有一个兼容WS-I的SOAP适配器,相同的修复程序适用于该文件。 You can find it in app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php . 您可以在app/code/core/Mage/Api/Model/Server/Wsi/Adapter/Soap.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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