Use AXIS2 to invoke SAP-generated webservice (with user password Authentication)

Source: Internet
Author: User
Tags auth soap wsdl

The project is exposed to the WebService interface generated with the SAP system, and the interface address is as follows:

Http://12.12.12.12:8000/sap/bc/srt/wsdl/srvc_55FDB689239112C0E10080000AF6031B/wsdl11/test/ws_policy/document? sap-client=400

This form of interface directly via Axis AXIS2 calls the address in a way that cannot be called successfully, reporting the following error

Axisfault
FaultCode: {http://schemas.xmlsoap.org/soap/envelope/}server.userexception
Faultsubcode:
FaultString:org.xml.sax.SAXException:Bad Envelope tag:definitions
Faultactor:
Faultnode:
Faultdetail:
{Http://xml.apache.org/axis/}stacktrace:org.xml.sax.saxexception:bad Envelope tag:definitions
At Org.apache.axis.message.EnvelopeBuilder.startElement (envelopebuilder.java:71)
At Org.apache.axis.encoding.DeserializationContext.startElement (deserializationcontext.java:1048)
At Com.sap.engine.lib.xml.parser.handlers.SAXDocHandler.startElementEnd (saxdochandler.java:144)
At Com.sap.engine.lib.xml.parser.XMLParser.scanElement (xmlparser.java:1782)
At Com.sap.engine.lib.xml.parser.XMLParser.scanDocument (xmlparser.java:2791)
At Com.sap.engine.lib.xml.parser.XMLParser.parse0 (xmlparser.java:227)
At Com.sap.engine.lib.xml.parser.AbstractXMLParser.parseAndCatchException (abstractxmlparser.java:141)
At Com.sap.engine.lib.xml.parser.AbstractXMLParser.parse (abstractxmlparser.java:156)
At Com.sap.engine.lib.xml.parser.AbstractXMLParser.parse (abstractxmlparser.java:259)
At Com.sap.engine.lib.xml.parser.Parser.parseWithoutSchemaValidationProcessing (parser.java:276)
At Com.sap.engine.lib.xml.parser.Parser.parse (parser.java:338)
At Com.sap.engine.lib.xml.parser.SAXParser.parse (saxparser.java:125)
At Javax.xml.parsers.SAXParser.parse (Unknown Source)
At Org.apache.axis.encoding.DeserializationContext.parse (deserializationcontext.java:227)
At Org.apache.axis.SOAPPart.getAsSOAPEnvelope (soappart.java:696)
At Org.apache.axis.Message.getSOAPEnvelope (message.java:435)
At Org.apache.axis.handlers.soap.MustUnderstandChecker.invoke (mustunderstandchecker.java:62)
At Org.apache.axis.client.AxisClient.invoke (axisclient.java:206)
At Org.apache.axis.client.Call.invokeEngine (call.java:2784)
At Org.apache.axis.client.Call.invoke (call.java:2767)
At Org.apache.axis.client.Call.invoke (call.java:2443)
At Org.apache.axis.client.Call.invoke (call.java:2366)
At Org.apache.axis.client.Call.invoke (call.java:1812)
At Test.wm.pickup.FeedBackStatusTest.main (feedbackstatustest.java:52)


The reason for this error should be that the generated interface does not conform to the WSDL specification.

WSDL Specification official explanation

WSDL specification Explained

WSDL specification

Service side in parsing the error, then there are other ways to call this type of SAP interface.

After a variety of searches on the web, we found that only AXIS2 generated local code.

1. First go to Apache official website download axis2, I download is axis2-1.5.6, unzip the file, then configure the win7 system environment variable, create a new axis2_home, and enter the AXIS2 path in the variable value "E:\axis2-1.5.6"

2. Access the SAP WebService address, right-save it as an XML file, and then change the suffix name to. WSDL, my file name is document.wsdl. Put it in the bin directory of Axis2 "E:\axis2-1.5.6\bin"

3. Generate the local code for the WSDL file. Open cmd, enter "E:\axis2-1.5.6\bin", enter command Wsdl2java-uri document.wsdl-p com.crm.sap.webservice.feedback-o Feedback

The Wsdl2java command uses

4. Create a Java project with Eclipse and copy the generated code to the project's SRC, as shown in


Bring all jars under the Axis2 directory (E:\axis2-1.5.6\lib) into the project

5. Create a class containing the main method to test, the code is as follows

<span style= "FONT-SIZE:14PX;"

>/** * * */package test;

Import Javax.xml.namespace.QName;
Import Org.apache.axiom.om.OMAbstractFactory;
Import org.apache.axiom.om.OMElement;
Import Org.apache.axiom.om.OMFactory;
Import Org.apache.axiom.om.OMNamespace;
Import org.apache.axis2.addressing.EndpointReference;
Import org.apache.axis2.client.Options;
Import org.apache.axis2.client.ServiceClient;
Import org.apache.axis2.rpc.client.RPCServiceClient;
Import org.apache.axis2.transport.http.HTTPConstants;

Import org.apache.axis2.transport.http.HttpTransportProperties;
Import com.crm.sap.webservice.feedback.ZTIHOSQStub;
Import com.crm.sap.webservice.feedback.ZTIHOSQStub.ZEMALL121;


Import Com.crm.sap.webservice.feedback.ZTIHOSQStub.ZEMALL121Response;

			public class Test {public static void main (string[] args) {try {ztihosqstub service = new Ztihosqstub ();
			ZEMALL121 params = new ZEMALL121 ();
			Zemall121response response = service.zemall121 (params); System.out.println (RESPONse.getev_type ());
		System.out.println (RESPONSE.GETEV_MESG ());
		} catch (Exception e) {e.printstacktrace (); }}} </span>

Running the code, the following error occurred:

Org.apache.axis2.AxisFault:Transport error:401 error:unauthorized
At Org.apache.axis2.transport.http.HTTPSender.handleResponse (httpsender.java:313)
At Org.apache.axis2.transport.http.HTTPSender.sendViaPost (httpsender.java:200)
At Org.apache.axis2.transport.http.HTTPSender.send (httpsender.java:76)
At Org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons ( commonshttptransportsender.java:400)
At Org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke (commonshttptransportsender.java:225)
At Org.apache.axis2.engine.AxisEngine.send (axisengine.java:438)
At Org.apache.axis2.description.OutInAxisOperationClient.send (outinaxisoperation.java:402)
At Org.apache.axis2.description.OutInAxisOperationClient.executeImpl (outinaxisoperation.java:229)
At Org.apache.axis2.client.OperationClient.execute (operationclient.java:165)
At com.crm.sap.webservice.feedback.ZTIHOSQStub.zEMALL121 (ztihosqstub.java:149)
At Test. Test.main (test.java:44)


The reason for the above error is because the interface requires user name password authentication, but in the generation of code is not generated to set the user name password Authentication code, then this need we manually added. You can add the authentication code to the following location

/** * * */package test;

Import Javax.xml.namespace.QName;
Import Org.apache.axiom.om.OMAbstractFactory;
Import org.apache.axiom.om.OMElement;
Import Org.apache.axiom.om.OMFactory;
Import Org.apache.axiom.om.OMNamespace;
Import org.apache.axis2.addressing.EndpointReference;
Import org.apache.axis2.client.Options;
Import org.apache.axis2.client.ServiceClient;
Import org.apache.axis2.rpc.client.RPCServiceClient;
Import org.apache.axis2.transport.http.HTTPConstants;

Import org.apache.axis2.transport.http.HttpTransportProperties;
Import com.crm.sap.webservice.feedback.ZTIHOSQStub;
Import com.crm.sap.webservice.feedback.ZTIHOSQStub.ZEMALL121;


Import Com.crm.sap.webservice.feedback.ZTIHOSQStub.ZEMALL121Response;
	                public class Test {public static void main (string[] args) {try {ztihosqstub service = new Ztihosqstub (); Authentication code start Httptransportproperties.authenticator auth = new httptransportproperties.
			Authenticator (); Auth.setusernamE ("* * * *");

			Auth.setpassword ("* * * *");
                       Service._getserviceclient (). GetOptions (). SetProperty (Httpconstants.authenticate, auth);
			Authentication code End ZEMALL121 params = new ZEMALL121 ();
			Zemall121response response = service.zemall121 (params);
			System.out.println (Response.getev_type ());
		System.out.println (RESPONSE.GETEV_MESG ());
		} catch (Exception e) {e.printstacktrace (); }}} </span>


can also in the Ztihosqstub class of the lowest construction method, see the source code discovery construction method is a layer, find the most low-level call construction method, join the user name Password authentication can ensure that other construction methods can also use the User name password authentication

<pre name= "code" class= "java" > Public ztihosqstub (org.apache.axis2.context.ConfigurationContext Configurationcontext, Java.lang.String Targetendpoint, Boolean Useseparatelistener) throws
		Org.apache.axis2.AxisFault {//To populate Axisservice populateaxisservice ();

		Populatefaults ();
            _serviceclient = new Org.apache.axis2.client.ServiceClient (Configurationcontext, _service);
	Authentication code start Httptransportproperties.authenticator auth = new Httptransportproperties.authenticator ();
		 <span style= "White-space:pre" > </span> auth.setusername ("* * *");

		Auth.setpassword ("* * * *");
Service._serviceclient.getoptions (). SetProperty (Httpconstants.authenticate, auth); <span> </span>//Authentication Code end _serviceclient.getoptions (). Setto (New Org.apache.axis2.addressing.EndpointReference (Targetendpoint)); _serviceclient.getoptions (). Setuseseparatelistener (Useseparatelistener);//Set the SOAP Version_serviceclient.getoptions (). Setsoapversionuri(Org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);} 






If the external network, the host and domain name must be added to
Basicauth.sethost ("xx.xx.x.xx");
Basicauth.setdomain ("xx.xx.xx.xx");



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.