C# 如何在 HTTPS (Magento 1.9.3.3) 中使用 SOAP V2 添加客户数据
C# How do I add customer data using SOAP V2 in HTTPS (Magento 1.9.3.3)
我正在开发一个 C# winapp 来将客户数据添加到 Magento 服务器。我的服务器使用的是 Magento 1.9.3.3。当我尝试使用 https
协议向服务器添加数据时遇到问题。如果使用 http
.
,下面的代码可以完美运行
这是我的 App.config
文件。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="httpsBinding" allowCookies="true" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Transport">
<transport clientCredentialType="None"
proxyCredentialType="None" realm=""/>
<message clientCredentialType="Certificate" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://192.168.1.72/magento/index.php/api/v2_soap/index/"
binding="basicHttpBinding" bindingConfiguration="httpsBinding"
contract="MagentoService.PortType" name="Port" />
</client>
</system.serviceModel>
</configuration>
这是创建客户的代码。
PortTypeClient mservice = new PortTypeClient();
var mlogin = mservice.login("SoapUser", "ApiKey"); //login
try
{
int newCustomerCreateID = 0;
filters myfilter = new filters();
// Create Customer
customerCustomerEntityToCreate customerCreate = new customerCustomerEntityToCreate();
customerCreate.email = "tarasdzxc@gmail.com";
customerCreate.password = "0000000000";
customerCreate.firstname = "tarawat";
customerCreate.lastname = "kingposs";
customerCreate.middlename = "";
customerCreate.store_id = 1;
customerCreate.store_idSpecified = true;
customerCreate.website_id = 1;
customerCreate.website_idSpecified = true;
customerCreate.group_id = 1;
customerCreate.group_idSpecified = true;
customerCreate.prefix = "MR";
customerCreate.dob = "10/01/1995";
customerCreate.gender = Int32.Parse("1"); //1-Male;2-Female
customerCreate.genderSpecified = true;
newCustomerCreateID =
mservice.customerCustomerCreate(mlogin, customerCreate);
MessageBox.Show(newCustomerCreateID.ToString());
mservice.endSession(mlogin);
}
catch (Exception ex)
{
//Unable to create account
MessageBox.Show(ex.ToString());
}
在我 运行 它之后它给了我这个错误:
System.ServiceModel.FaultException: 'SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1' : failed to load external entity "https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1"'
如果我将安全模式更改为 http
,它可以完美运行,但我需要使用 https
。
我尝试复制 url https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1 and enter to browser results is show XML like this。
最后,我使用的是 Apache 2.4 的 OpenSSL,我已经允许我的 php.ini
进行 SSL 扩展,但在我的 localhost
中说它不安全。
更新:仍然无效。我从 Rom Eh 的回答中得到了这个错误
System.ServiceModel.ProtocolException: 'The content type text/xml; charset=utf-8 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 470 bytes of the response were: '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>WSDL</faultcode><faultstring>SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://192.168.1.72/magento2/index.php/api/v2_soap/index/?wsdl=1' : failed to load external entity "https://192.168.1.72/magento2/index.php/api/v2_soap/index/?wsdl=1"
</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
OP 是我的同事,我们已经在几天前解决了这个问题,我将此发布给对此问题感到好奇的人。
首先,他的app.config
绝对没问题。 basicHttpBinding
与 Transport
模式可以与 HTTPS
一起使用,但 实际问题是他的 SSL 是 self-signed 证书 。来自 Visual Studio 的错误报告没有提供足够的信息,您还必须查看 Apache 错误日志 (error.log)。它总是在下面抛出这些错误
SoapServer::SoapServer(): SSL operation failed with code 1. OpenSSL Error messages:\nerror:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
SoapServer::SoapServer(): Failed to enable crypto in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
SoapServer::SoapServer(): I/O warning : failed to load external entity "https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1" in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1' : failed to load external entity "https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1"\n in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
显示问题是OpenSSL未能验证他的证书。所以我们更改了使用合法 SSL 的测试环境,一切正常。
我正在开发一个 C# winapp 来将客户数据添加到 Magento 服务器。我的服务器使用的是 Magento 1.9.3.3。当我尝试使用 https
协议向服务器添加数据时遇到问题。如果使用 http
.
这是我的 App.config
文件。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="httpsBinding" allowCookies="true" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="Transport">
<transport clientCredentialType="None"
proxyCredentialType="None" realm=""/>
<message clientCredentialType="Certificate" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://192.168.1.72/magento/index.php/api/v2_soap/index/"
binding="basicHttpBinding" bindingConfiguration="httpsBinding"
contract="MagentoService.PortType" name="Port" />
</client>
</system.serviceModel>
</configuration>
这是创建客户的代码。
PortTypeClient mservice = new PortTypeClient();
var mlogin = mservice.login("SoapUser", "ApiKey"); //login
try
{
int newCustomerCreateID = 0;
filters myfilter = new filters();
// Create Customer
customerCustomerEntityToCreate customerCreate = new customerCustomerEntityToCreate();
customerCreate.email = "tarasdzxc@gmail.com";
customerCreate.password = "0000000000";
customerCreate.firstname = "tarawat";
customerCreate.lastname = "kingposs";
customerCreate.middlename = "";
customerCreate.store_id = 1;
customerCreate.store_idSpecified = true;
customerCreate.website_id = 1;
customerCreate.website_idSpecified = true;
customerCreate.group_id = 1;
customerCreate.group_idSpecified = true;
customerCreate.prefix = "MR";
customerCreate.dob = "10/01/1995";
customerCreate.gender = Int32.Parse("1"); //1-Male;2-Female
customerCreate.genderSpecified = true;
newCustomerCreateID =
mservice.customerCustomerCreate(mlogin, customerCreate);
MessageBox.Show(newCustomerCreateID.ToString());
mservice.endSession(mlogin);
}
catch (Exception ex)
{
//Unable to create account
MessageBox.Show(ex.ToString());
}
在我 运行 它之后它给了我这个错误:
System.ServiceModel.FaultException: 'SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1' : failed to load external entity "https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1"'
如果我将安全模式更改为 http
,它可以完美运行,但我需要使用 https
。
我尝试复制 url https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1 and enter to browser results is show XML like this。
最后,我使用的是 Apache 2.4 的 OpenSSL,我已经允许我的 php.ini
进行 SSL 扩展,但在我的 localhost
中说它不安全。
更新:仍然无效。我从 Rom Eh 的回答中得到了这个错误
System.ServiceModel.ProtocolException: 'The content type text/xml; charset=utf-8 of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 470 bytes of the response were: '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>WSDL</faultcode><faultstring>SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://192.168.1.72/magento2/index.php/api/v2_soap/index/?wsdl=1' : failed to load external entity "https://192.168.1.72/magento2/index.php/api/v2_soap/index/?wsdl=1"
</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
OP 是我的同事,我们已经在几天前解决了这个问题,我将此发布给对此问题感到好奇的人。
首先,他的app.config
绝对没问题。 basicHttpBinding
与 Transport
模式可以与 HTTPS
一起使用,但 实际问题是他的 SSL 是 self-signed 证书 。来自 Visual Studio 的错误报告没有提供足够的信息,您还必须查看 Apache 错误日志 (error.log)。它总是在下面抛出这些错误
SoapServer::SoapServer(): SSL operation failed with code 1. OpenSSL Error messages:\nerror:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
SoapServer::SoapServer(): Failed to enable crypto in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
SoapServer::SoapServer(): I/O warning : failed to load external entity "https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1" in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1' : failed to load external entity "https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1"\n in C:\xampp\htdocs\magento\lib\Zend\Soap\Server.php on line 814
显示问题是OpenSSL未能验证他的证书。所以我们更改了使用合法 SSL 的测试环境,一切正常。