JAVA methods for invoking Web service
1. Using HttpClient
Jar file used: Commons-httpclient-3.1.jar
Method:
Pre-defined SOAP request data can be generated with the help of the XMLSpy professional software.
String Soaprequestdata="<?xml version=\"1.0\"Encoding=\"Utf-8\"?>"+
"<soap12:envelope xmlns:xsi=\"http//Www.w3.org/2001/xmlschema-instance\ "Xmlns:xsd=\"Http://www.w3.org/2001/XMLSchema\ "Xmlns:soap12=\"Http://www.w3.org/2003/05/soap-envelope\ ">" +
"<soap12:Body>"+
"<getcountrycitybyip xmlns=\"http//Webxml.com.cn/\ ">" +
"<theIpAddress>219.137.167.157</theIpAddress> " +
" </getcountrycitybyip> " +
</soap12:body> " +
"</soap12:envelope>"
A postmethod is then defined, and the URL of the Web service needs to be specified;
postmethod Postmethod = new postmethod ("http://www.webxml.com.cn/WebServices/ Ipaddresssearchwebservice.asmx ");
Then add the SOAP request data to the Postmethod
Byte[] B= soaprequestdata.getbytes ( "utf-8 ");
Inputstream is = new bytearrayinputstream (B,0,b.length);
Requestentity re = new inputstreamrequestentity (Is,b.length, " Span style= "color: #000000;" >application/soap+xml; charset=utf-8 ");
Postmethod.setrequestentity (re);
Finally, a HttpClient object is generated and a Postmethod request is issued
HttpClient HttpClient = new HttpClient ();
StatusCode = Httpclient.executemethod (postmethod);
String soaprequestdata = postmethod.getresponsebodyasstring ();
Soaprequestdata is the SOAP response data that invokes the Web service, which is in XML format and can be parsed soaprequestdata to get the return value of the calling Web service.
2. Using Xfire
Jar file used by Xfire-all-1.2.4.jar, Jdom-1.0.jar
Method:
Defines a client object that specifies the address of the WSDL for the Web service
Client C = new Client ( the New URL ("http:///www.webxml.com.cn/WebServices/ IPADDRESSSEARCHWEBSERVICE.ASMX?WSDL "));
Call the client object's Invoke method, specify the Web service's method name, and the parameter, and the return value is an array of type object.
The following code calls the Getversiontime method, which does not have parameters that use the new object[0] for a later parameter.
object[] Results = C.invoke ("Getversiontime", new object[0]);
3. Using Axis2
Download axis2-1.4
Method:
Open the console and go to the Axis2-1.4/bin directory
wsdl2java. Bat -uri http://www webxml. Com. Cn/webservices/ipaddresssearchwebservice. Asmx?wsdl -p ws. Clinet. Axis2
After the above command is executed, a src directory is generated in the current directory, and Xxxxcallbackhandler.java and Xxxxstub.java two files are generated in the src\ ws\ clinet\ axis2 directory.
Wsdl2java generates a class of call interfaces, parameter classes, and return values for the Web service based on the WSDL file.
It is possible to instantiate a Xxxxstub object directly when calling WebService, and then invoke the Web service's method.
4. Summary
A Web service service that is built for a tool may have a simpler calling method than the one that is described here, and the methods described above for invoking Web services are generic.
The above three methods used httpclient should be more flexible, but the development of low efficiency, difficult, the use of Xfire and Axis2 relatively easy, rapid development, but AXIS2 universality is not good, some Web services with Axis2 bad use. HttpClient and Xfire versatility is better, in view of the above features recommend the use of Xfire.
JAVA methods for invoking Web service (reproduced)