简体   繁体   中英

C# How do I add customer data using SOAP V2 in HTTPS (Magento 1.9.3.3)

I'm developing a C# winapp to add customer data to Magento server. My server is using Magento 1.9.3.3. I have a problem when I tried to add data to the server, using https protocol. The code below works flawlessly if using http .

This is my App.config file.

<?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>

and this is code for create customer.

    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());
            }

after I ran it and it gave me this error:

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"'

If I change the security mode to http , it works flawlessly but I need to use https . I tried copy 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 .

Lastly, I'm using OpenSSL from Apache 2.4 and I already allowed SSL extension from my php.ini but in my localhost said it unsecure.

UPDATE : Still doesn't work. I got this error from Rom Eh's answer

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 is my coworker and we already fixed this issue days ago and I'm posting this for people who curious about this issue.

Firstly, his app.config is absolutely fine . The basicHttpBinding with Transport mode can be used with HTTPS but the actual problem is his SSL is a self-signed certificate . The error report from Visual Studio isn't giving enough info, you must look at the Apache error log (error.log) too. It always throws these error below

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 &quot;https://192.168.1.72/magento/index.php/api/v2_soap/index/?wsdl=1&quot; 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

It showed the problem was OpenSSL failed to verify his certificate. So we changed the test environment that uses legit SSL and everything works without any problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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