在 C# 中,当连接到 Magento API 时,它会在成功登录时抛出异常:
// connect to the first store
_magentoService = new MagentoService {Url = "http://store1.com/index.php/api/v2_soap"};
_magentoService.login(user1, password1);
// later on, connect to the second store (note the different url)
_magentoService = new MagentoService {Url = "http://store2.com/index.php/api/v2_soap"};
_magentoService.login(user2, password2);
连接到第一家商店返回:
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:loginResponse><loginReturn xsi:type="xsd:string">bec0144c8642f227e8fb3d032f40xxxx</loginReturn></ns1:loginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
这是成功登录所期望的确切响应。连接到第二个商店也会返回完全相同的结果,并且还会引发异常:
System.Net.WebException was caught
Message=The request failed with the error message:
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:loginResponse><loginReturn xsi:type="xsd:string">bec0144c8642f227e8fb3d032f40zzzz</loginReturn></ns1:loginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Source=System.Web.Services
StackTrace:
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
为什么登录成功后会抛出异常?第一个站点(成功)是 Magento v1.6,失败的商店是 Magento v1.7
我通过网络参考使用 Magento,login() 方法的生成代码如下。调用 this.Invoke 时发生异常:
[System.Web.Services.Protocols.SoapRpcMethodAttribute("urn:Mage_Api_Model_Server_V2_HandlerAction", RequestNamespace="urn:Magento", ResponseNamespace="urn:Magento")]
[return: System.Xml.Serialization.SoapElementAttribute("loginReturn")]
public string login(string username, string apiKey) {
object[] results = this.Invoke("login", new object[] {
username,
apiKey});
return ((string)(results[0]));
}