簡體   English   中英

在C#中調用Magento SOAP V2服務的問題

[英]Problems calling Magento SOAP V2 Service in C#

我正在使用IIS 7.5和PHP 5.6(測試)在本地計算機上托管一個magento網上商店。 商店工作得很好,但現在我想使用Visual Studio 2013創建一個單獨的應用程序。這些是我已經采取的步驟:

  1. 我已將域名“ www.domain.com.local”添加到指向我的本地主機的主機文件中(www.domain.com.local-> 127.0.0.1)
  2. 我在IIS上創建了一個新網站,並添加了新的綁定(www.domain.com.local-參見1)
  3. 我在PHP Manager中添加了WinCache擴展,並將PHP版本設置為5.6。
  4. 在magento后端中啟用WS-I遵從性(系統>配置> Magento Core 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-反序列化回復消息正文時出錯(Magento API)
  2. C#+ Magento API V2:內容類型為text / xml; 字符集= UTF-8,文本/ XML; 響應消息的charset = UTF-8不匹配

如果從Magento Stack Exchange的 Rian那里得到了這個答案。 所有學分歸Rian所有


解決方案:

您遇到的問題是.NET / C#無法解析Magento隨其響應發送的內容類型。 眾所周知,SOAP以正確的格式接收正確的內容是挑剔的。 將其與PHP協議的較差實現相結合,您將獲得很多樂趣。

我正在尋找Magento 1.9,以獲取以下信息:

經過一番挖掘后,我發現在第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...

請注意,Content-Type標頭與您的text/xml; charset=utf-8相匹配text/xml; charset=utf-8 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM