Axis2 client calls free webservice instance 2 manual call free

Source: Internet
Author: User
In the previous section, axis2 generates a client program using wsdl and calls it locally. This method requires you to configure environment variables, run the doscommand, and then compile the output code based on the generated code, which is more complex than that. This section describes how to use axis2 to manually call the online free webservice Service. This method is simpler. You only need to introduce axis

In the previous section, axis2 generates a client program using wsdl and calls it locally. This method requires you to configure environment variables, run the doscommand, and then compile the output code based on the generated code, which is more complex than that. This section describes how to use axis2 to manually call the online free webservice Service. This method is simpler. You only need to introduce axis

In the previous section, axis2 generates a client program using wsdl and calls it locally. This method requires you to configure environment variables, run the doscommand, and then compile the output code based on the generated code, which is more complex than that. This section describes how to use axis2 to manually call the online free webservice Service. This method is simpler. You only need to create a java class after introducing the axis2 package to achieve our goal.

The project structure is as follows:

After introducing the jar package of axis2, you only need to create a java class testWebService2. The following describes the code in this class.

Note: In the code, you must first run the. wsdl service address link in the browser to check whether the link can be opened successfully before it can be used in the code.

Package com. test. weather; import java. util. iterator; 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. axiom. soap. SOAP11Constants; import org. apache. axis2.Constants; import org. apache. axis2.addressing. endpointReference; import org. apache. axis2.client. options; import org. apache. axis2.client. serviceClient; public class testWebService2 {public static void main (String [] args) throws Exception {// axis2 ServiceClient sender = new ServiceClient () by using the free webservice on the Internet (); options option = new Options (); option. setSoapVersionURI (SOAP11Constants. SOAP_ENVELOPE_NAMESPACE_URI); option. setAction (" http://WebXml.com.cn/ GetWeather "); EndpointReference epfs = new EndpointReference (" http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl "); Option. setTransportInProtocol (Constants. TRANSPORT_HTTP); option. setTo (epfs); sender. setOptions (option); OMFactory fac = OMAbstractFactory. getOMFactory (); OMNamespace omNs = fac. createOMNamespace (" http://WebXml.com.cn/ "," "); OMElement data = fac. createOMElement ("getWeather", omNs); OMElement theCityCode = fac. createOMElement ("theCityCode", omNs); OMElement theUserID = fac. createOMElement ("theUserID", omNs); theCityCode. setText ("Beijing"); theUserID. setText (""); data. addChild (theCityCode); data. addChild (theUserID); OMElement result = sender. sendReceive (data); // System. out. println (result); // ---------------- Iterator in = result. getChildrenWithLocalName ("getWeatherResult"); while (in. hasNext () {OMElement om = (OMElement) in. next (); Iterator in2 = om. getChildElements (); while (in2.hasNext () {System. out. println (in2.next (). toString (); // System. out. println (OMElement) in2.next ()). getText ());}}}}
This app is used by axis2 to provide free webservice for weather conditions in a city.

Running code ------ the above Code will get the result



The result shows that the information is in xml format. If you want to remove the node and output the information directly, you need to annotate and run the code below ------ In the code. The result is as follows:



The introduction of using axis2 to manually call the free webservice on the internet is complete. Compared with the method in the previous section, the two methods have their own advantages and disadvantages. The method in the previous section is more complex. This method is simple, however, it depends heavily on the network, so you must run it in the browser. the service address link of wsdl. It can be used in your own code after it is opened successfully.

Some other methods were found during the study of axis2's access to the webservice, but some errors were reported during the operation. The specific situation will be described in the next section.

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.