Neo Jun met the situation that he needed to call the web service interface. However, as a little white, he was tired of understanding this aspect. After groping, he completed a small Demo and recorded it.
1: background
My colleague gave me a web service address. After opening it, the suffix is "...? wsdl". Put the link "http: / /...? wsdl" into soapUI, and the complete one can be parsed. But Mr. Neo was blank here before. After searching the Internet for some basic knowledge of web service, soap and wsdl, he learned something about it. But when it comes to code, many of the content found on the Internet is copy and incomplete.
Two: Code
Don't say much, take the detour in the middle and stick the code directly.
1 import org.apache.axis.client.Call; 2 import org.apache.axis.client.Service; 3 import org.apache.axis.encoding.XMLType; 4 import org.apache.axis.message.MessageElement; 5 import org.apache.axis.message.SOAPHeaderElement; 6 import org.apache.axis.types.Schema; 7 import org.dom4j.Document; 8 import org.dom4j.io.SAXReader; 9 10 import javax.xml.namespace.QName; 11 import javax.xml.soap.SOAPElement; 12 import javax.xml.soap.SOAPFactory; 13 import java.io.ByteArrayInputStream; 14 15 16 public class WebserviceUtilTest { 17 public static void main(String[] args) { 18 try{ 19 String endpoint="http://xxx"; 20 // Direct reference remote wsdl file,"?wsdl"Previous links 21 String method="test"; 22 String namespace = "http://www.xxxx"; 23 24 Service service = new Service(); 25 Call call = (Call) service.createCall(); 26 //Set server address 27 call.setTargetEndpointAddress(new java.net.URL(endpoint)); 28 //Set namespace and call method 29 call.setOperationName(new QName(namespace,method));// WSDL Interface name described in 30 call.setUseSOAPAction(true); 31 32 33 //Set up header,If there is security Demand words 34 SOAPHeaderElement soapHeaderElement = createHeader(); 35 if(soapHeaderElement == null){ 36 System.out.println("====Exception"); 37 return; 38 } 39 call.addHeader(soapHeaderElement); 40 41 //Add the parameters of the method, including several 42 call.addParameter("param1", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);// Parameters of the interface 43 call.addParameter("param2", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);// Parameters of the interface 44 //Set return type 45 46 /*The return type can not be set. If it is a simple type, you can use simple types such as xmltype.xsd ﹣ string. Here, you use schema to parse the returned message. 47 If the received type is inconsistent with the set type, the following exception will be reported: 48 SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.*/ 49 call.setReturnType(XMLType.XSD_SCHEMA);// Set return type 50 51 52 53 String param1Value = "test"; 54 String param2Value = "test"; 55 Schema result = (Schema) call.invoke(new Object[] { account,passwd }); 56 57 //Parsing return results 58 MessageElement[] data = result.get_any(); 59 for(int i=0; i<data.length; i++){ 60 SAXReader reader = new SAXReader(); 61 Document doc = reader.read(new ByteArrayInputStream(data[i].toString().getBytes())); 62 System.out.println(doc.getStringValue()); 63 } 64 65 66 //output SOAP Request message 67 System.out.println("--SOAP Request: " + call.getMessageContext().getRequestMessage().getSOAPPartAsString()); 68 //output SOAP Return message 69 System.out.println("--SOAP Response: " + call.getResponseMessage().getSOAPPartAsString()); 70 71 72 }catch (Exception e){ 73 e.printStackTrace(); 74 75 } 76 } 77 /*Construction of Header*/ 78 public static SOAPHeaderElement createHeader(){ 79 String AUTH_PREFIX = "wsse"; 80 String AUTH_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; 81 String TYPE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"; 82 83 String userName= "admin"; 84 String passwd= "admin"; 85 86 87 //SOAPHeaderElement soapHeaderElement =new SOAPHeaderElement(); 88 89 90 try{ 91 SOAPFactory soapFactory =SOAPFactory.newInstance(); 92 SOAPElement wsSecHeaderElm = soapFactory.createElement("Security", AUTH_PREFIX, AUTH_NS); 93 SOAPElement userNameTokenElm = soapFactory.createElement("UsernameToken",AUTH_PREFIX, AUTH_NS); 94 SOAPElement userNameElm = soapFactory.createElement("Username",AUTH_PREFIX, AUTH_NS); 95 SOAPElement passwdElm = soapFactory.createElement("Password",AUTH_PREFIX, AUTH_NS); 96 passwdElm.setAttribute("Type", TYPE); 97 98 userNameElm.addTextNode(userName); 99 passwdElm.addTextNode(passwd); 100 101 userNameTokenElm.addChildElement(userNameElm); 102 userNameTokenElm.addChildElement(passwdElm); 103 wsSecHeaderElm.addChildElement(userNameTokenElm); 104 105 SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(wsSecHeaderElm); 106 soapHeaderElement.setMustUnderstand(true); 107 108 return soapHeaderElement; 109 }catch(Exception e) { 110 e.printStackTrace(); 111 return null; 112 } 113 } 114 }