[英]Problems calling Magento SOAP V2 Service in C#
我正在使用IIS 7.5和PHP 5.6(測試)在本地計算機上托管一個magento網上商店。 商店工作得很好,但現在我想使用Visual Studio 2013創建一個單獨的應用程序。這些是我已經采取的步驟:
這是我的代碼:
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方法)。
我已閱讀並嘗試過的相關主題(沒有成功):
如果從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.