Axis2 calls the asmx WebService written by. net to query weather instances through the city name.

Source: Internet
Author: User

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:

 

Related Article

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.