Annotations are your own understanding ..
Package com. jielan. axis2;
Import org. apache. axiom. om. omw.actfactory;
Import org. apache. axiom. om. OMElement;
Import org. apache. axiom. om. OMFactory;
Import org. apache. axiom. om. OMNamespace;
Import org. apache. axis2.AxisFault;
Import org. apache. axis2.addressing. EndpointReference;
Import org. apache. axis2.client. Options;
Import org. apache. axis2.client. ServiceClient;
Import org. apache. axis2.transport. http. HTTPConstants;
Public class Client01 {
Private static String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx ";
// Endpoint Reference refers to the interface location
Private static EndpointReference targetEpr = new EndpointReference (url );
// Abstract The OM factory to obtain the OM factory and create the request SOAP package
Private static OMFactory fac = omdomainactfactory. getOMFactory ();
Public static OMElement getow.hod (String methodStr, String namespace, String tns, String [] pars, String [] vals ){
// Create a namespace
OMNamespace nms = fac. createOMNamespace (namespace, tns );
// Create an OMElement method element and specify it in the namespace referred to by nms
OMElement method = fac. createOMElement (methodStr, nms );
// Add the method parameter name and Value
For (int I = 0; I <pars. length; I ++ ){
// Create a method parameter OMElement
OMElement param = fac. createOMElement (pars [I], nms );
// Set the key-Value Pair parameter value
Param. setText (vals [I]);
// Add the method element to the method Element
Method. addChild (param );
}
Return method;
}
Public static Options getClientOptions (String action ){
// CREATE request soap package request options
Options options = new Options ();
// Set soapAction for options
Options. setAction (action );
// Set the request soap package endpoint reference (interface address)
Options. setTo (targetEpr );
// If the error message "Content-Length" is returned, the Length of the request Content
Options. setProperty (HTTPConstants. CHUNKED, "false"); // Content-Length is automatically added after chunk is disabled.
Return options;
}
Public static OMElement getWeather (String action, String methodStr, String namespace, String tns, String [] pars, String [] vals ){
OMElement result = null;
Try {
ServiceClient client = new ServiceClient ();
Client. setOptions (getClientOptions (action ));
Result = client. sendReceive (getow.hod (methodStr, namespace, tns, pars, vals ));
} Catch (AxisFault e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return result;
}
Public static void main (String [] args ){
String action = "http://WebXml.com.cn/getWeatherbyCityName ";
String methodStr = "getWeatherbyCityName ";
String namespace = "http://WebXml.com.cn /";
String tns = "xsd ";
String [] pars = {"theCityName "};
String [] vals = {"Hangzhou "};
OMElement result = null;
Result = getWeather (action, methodStr, namespace, tns, pars, vals );
System. out. println (result );
}
}
View namespace
View the endpoint reference interface address
View the call method name and soapAction action address
In addition, OMElement method = fac. createOMElement (methodStr, nms); the second parameter, nms, seems to be left blank.
However, I found that some people say this is the service name ..
If you do not enter an existing variable name and encounter a variable definition error, you can ..
Result:
Call on index. jsp
<Body>
<%
// Out. print (InvokeExercise. invokeWucan (). replaceAll ("</string>", "</string> \ r <br/> "));
// Out. print (InvokeExercise. invokeYouCan ());
Out. print (Client01.getWeather (
"Http://WebXml.com.cn/getWeatherbyCityName ",
"GetWeatherbyCityName", "http://WebXml.com.cn /",
"Xsd", new String [] {"theCityName "},
New String [] {"Taizhou"}). toString (). replaceAll (". ",". <Br/> "). replaceAll (" weather today "," <br/> weather today "));
%>
</Body>
Web call results: