Java – code generated by soap UI
I have a web service and I'm trying to build a client
I have the following WSDL:
http://www.cmicdataservices.com/datacenter/service.asmx?wsdl
It requires authentication Looking at the WSDL description, I can't see the method of using authentication object, nor does it use user name and password as parameters Using NetBeans, I have generated Jax WS source code for WSDL However, I can't figure out what to do after that
Using soap UI, I can connect to the web service and run all the methods But again, I want to build it into a client that can run without my interaction
My problem is figuring out how to use this generated code, which looks like NetBeans TV has a video (NetBeans soapUI plug-in Video 2), which was later lost Does anyone know any tutorials or any examples of how to use this generated code to access Web services?
So I have a method checkifauthorized ()
Running in soap UI, I get the following XML
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:cmic="http://www.cmicdataservices.com/"> <soap:Header> <cmic:Authentication> <!--Optional:--> <cmic:UserName>username</cmic:UserName> <!--Optional:--> <cmic:Password>password</cmic:Password> </cmic:Authentication> </soap:Header> <soap:Body> <cmic:CheckIfAuthorized/> </soap:Body> </soap:Envelope>
Then, I can run the request in the soap UI and get a successful authentication response
Using the Jax WS code generated by NetBeans and soapUI, I have the following:
package javaapplication7; /** * * @author grant */ public class Main { public static void main(String[] args) { Boolean result = checkIfAuthorized(); System.out.println("The result is: " + result); } private static boolean checkIfAuthorized() { javaapplication7.CMICDatacenterService service = new javaapplication7.CMICDatacenterService(); javaapplication7.CMICDatacenterServiceSoap port = service.getCMICDatacenterServiceSoap(); return port.checkIfAuthorized(); } }
This will fail with the following error
run: Exception in thread "main" javax.xml.ws.soap.soAPFaultException: Server was unable to process request. ---> Object reference not set to an instance of an object. at com.sun.xml.internal.ws.fault.soAP11Fault.getProtocolException(SOAP11Fault.java:178) at com.sun.xml.internal.ws.fault.soAPFaultBuilder.createException(SOAPFaultBuilder.java:111) at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108) at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78) at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107) at $Proxy30.checkIfAuthorized(UnkNown Source) at javaapplication7.Main.checkIfAuthorized(Main.java:24) at javaapplication7.Main.main(Main.java:17) Java Result: 1
This is the same problem I encountered when trying to use Python for services I have chosen to use Java because I think I can parse XML and create objects faster because I have created this entity
thank you.
Grant
I didn't want to answer this question because I still wanted to know what I could do here, but I finally wrote the request by hand Now I can convert it into an XML object and do it my way, but I think soapUI makes it easier What I really don't understand is how to use soapUI to build this request and incorporate it into my project:
public class Main { public final static String DEFAULT_SERVER = "http://www.cmicdataservices.com/datacenter/service.asmx"; public final static String SOAP_ACTION = "http://www.cmicdataservices.com/CheckIfAuthorized"; public static void main(String[] args) { String server = DEFAULT_SERVER; String UserName = "Username"; String Password="Password"; try{ URL url = new URL(server); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8"); connection.setRequestProperty("Host","www.cmicdataservices.com"); OutputStream out = connection.getOutputStream(); Writer wout = new OutputStreamWriter(out); // Uncomment the following and comment out the prevIoUs two lines to see your xml //BufferedWriter wout = new BufferedWriter(new FileWriter("/tmp/testXML.xml")); //Start writing soap request - Envelope wout.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"); wout.write("<soap12:Envelope "); wout.write("xmlns:xsi="); wout.write("'http://www.w3.org/2001/XMLSchema-instance' "); wout.write("xmlns:xsd="); wout.write("'http://www.w3.org/2001/XMLSchema' "); wout.write("xmlns:soap12="); wout.write("'http://www.w3.org/2003/05/soap-envelope'>\r\n"); //Soap request header start wout.write("<soap12:Header>\r\n"); //Start writing soap request - Authentication wout.write("<Authentication xmlns="); wout.write("'http://www.cmicdataservices.com/'>\r\n"); wout.write("<UserName>" + UserName + "</UserName>\r\n"); wout.write("<Password>" + Password + "</Password>\r\n"); // End Authentication wout.write("</Authentication>\r\n"); //End the header wout.write("</soap12:Header>\r\n"); //Start writing the body wout.write("<soap12:Body>"); wout.write("<GetCurrentDataVer1 xmlns="); wout.write("'http://www.cmicdataservices.com/' />\r\n"); // End the Body wout.write("</soap12:Body>\r\n"); // End the Envelope wout.write("</soap12:Envelope>\r\n"); wout.flush(); wout.close(); //BufferedWriter fout = new BufferedWriter(new FileWriter("/tmp/testXMLResponse.xml")); InputStream in = connection.getInputStream(); createFile(in,"/tmp/testXMLResponse.xml"); } catch (IOException e) { System.err.println(e); } } public static void createFile(InputStream io,String fileName) throws IOException { FileOutputStream fout = new FileOutputStream(fileName); byte[] buf = new byte[256]; int read = 0; while ((read = io.read(buf)) != -1){ fout.write(buf,read); } }
Solution
The problem with your code is that the authentication element is missing from the soap header Looking at WSDL, it should always exist:
<wsdl:operation name="CheckIfAuthorized"> <soap:operation soapAction="http://www.cmicdataservices.com/CheckIfAuthorized" style="document"/> <wsdl:input> <soap:body use="literal"/> <soap:header message="tns:CheckIfAuthorizedAuthentication" part="Authentication" use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation>
When there is no authentication element in the checkifauthorized request, the server will fail due to an exception due to incorrect XML The following is a sample client in Python to demonstrate the idea of solving the problem. I don't think it's a problem to convert it to Java
from suds.client import Client client = Client("http://www.cmicdataservices.com/datacenter/service.asmx?wsdl") auth = client.factory.create('Authentication') auth.UserName = "username" auth.Password = "password" client.set_options(soapheaders=auth) client.service.CheckIfAuthorized()