I am new in calling a SOAP Web service API. How do I call a Magento Soap Client API using Wsdl uri with a Username and Password in java?
Also I have tried the following examples:
But I didn't get the proper output like a Product List.
public class MegentoCategory {
String user = "xxx";
String pass = "xxx";
String host = "http://www.xxx.co.xx/index.php/api/soap/?wsdl";
public MegentoCategory() throws MalformedURLException
{
try{
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), host);
// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();
}
catch(Exception e)
{
e.getMessage();
}
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelopexmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body><mag:loginxmlns:mag='urn:Magento'<mag:username>xxx</mag:username<mag:apiKey>xxx</mag:apiKey></mag:login></soapenv:Body></soapenv:Envelope>";
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "login");
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
}
Response :
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:typens="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
name="Magento" targetNamespace="urn:Magento">
<types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
<!-- <import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />-->
<complexType name="FixedArray">
<complexContent>
<restriction base="soapenc:Array">
<attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:anyType[]" />
</restriction>
</complexContent>
</complexType>
</schema>
</types>
<message name="call">
<part name="sessionId" type="xsd:string" />
<part name="resourcePath" type="xsd:string" />
<part name="args" type="xsd:anyType" />
</message>
<message name="callResponse">
<part name="callReturn" type="xsd:anyType" />
</message>
<message name="multiCall">
<part name="sessionId" type="xsd:string" />
<part name="calls" type="typens:FixedArray" />
<part name="options" type="xsd:anyType" />
</message>
<message name="multiCallResponse">
<part name="multiCallReturn" type="typens:FixedArray" />
</message>
<message name="endSession">
<part name="sessionId" type="xsd:string" />
</message>
<message name="endSessionResponse">
<part name="endSessionReturn" type="xsd:boolean" />
</message>
<message name="login">
<part name="username" type="xsd:string" />
<part name="apiKey" type="xsd:string" />
</message>
<message name="loginResponse">
<part name="loginReturn" type="xsd:string" />
</message>
<message name="resources">
<part name="sessionId" type="xsd:string" />
</message>
<message name="resourcesResponse">
<part name="resourcesReturn" type="typens:FixedArray" />
</message>
<message name="globalFaults">
<part name="sessionId" type="xsd:string" />
</message>
<message name="globalFaultsResponse">
<part name="globalFaultsReturn" type="typens:FixedArray" />
</message>
<message name="resourceFaults">
<part name="resourceName" type="xsd:string" />
<part name="sessionId" type="xsd:string" />
</message>
<message name="resourceFaultsResponse">
<part name="resourceFaultsReturn" type="typens:FixedArray" />
</message>
<message name="startSession" />
<message name="startSessionResponse">
<part name="startSessionReturn" type="xsd:string" />
</message>
<portType name="PortType">
<operation name="call">
<documentation>Call api functionality</documentation>
<input message="typens:call" />
<output message="typens:callResponse" />
</operation>
<operation name="multiCall">
<documentation>Multiple calls of resource functionality</documentation>
<input message="typens:multiCall" />
<output message="typens:multiCallResponse" />
</operation>
<operation name="endSession">
<documentation>End web service session</documentation>
<input message="typens:endSession" />
<output message="typens:endSessionResponse" />
</operation>
<operation name="login">
<documentation>Login user and retrive session id</documentation>
<input message="typens:login" />
<output message="typens:loginResponse" />
</operation>
<operation name="startSession">
<documentation>Start web service session</documentation>
<input message="typens:startSession" />
<output message="typens:startSessionResponse" />
</operation>
<operation name="resources">
<documentation>List of available resources</documentation>
<input message="typens:resources" />
<output message="typens:resourcesResponse" />
</operation>
<operation name="globalFaults">
<documentation>List of resource faults</documentation>
<input message="typens:globalFaults" />
<output message="typens:globalFaultsResponse" />
</operation>
<operation name="resourceFaults">
<documentation>List of global faults</documentation>
<input message="typens:resourceFaults" />
<output message="typens:resourceFaultsResponse" />
</operation>
</portType>
<binding name="Binding" type="typens:PortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="call">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="multiCall">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="endSession">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="login">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="startSession">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="resources">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="globalFaults">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="resourceFaults">
<soap:operation soapAction="urn:Action" />
<input>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body namespace="urn:Magento" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="MagentoService">
<port name="Port" binding="typens:Binding">
<soap:address location="http://www.gsequestrian.co.uk/index.php/api/soap/index/" />
</port>
</service>
</definitions>