Java + Method for calling Web + Service

Source: Internet
Author: User

 

1. Use httpclient
Jar files used: commons-httpclient-3.1.jar
Method:
Pre-defined SOAP request data can be generated using xmlspy professional software.

String soaprequestdata = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+
"<Soap12: envelope xmlns: xsi = \" comment "xmlns: XSD = \" http://www.w3.org/2001/xmlschema\ "xmlns: soap12 = \" comment ">" +
"<Soap12: Body>" +
"<Getcountrycitybyip xmlns = \" http://WebXml.com.cn/\ ">" +
"<Theipaddress> 219.137.167.157 </theipaddress>" +
"</Getcountrycitybyip>" +
"</Soap12: Body>" +
"</Soap12: envelope> ";

Define a postmethod. In this case, you must specify the URL of the web service;

Postmethod = new postmethod ("http://www.webxml.com.cn/webservices/ipaddresssearchwebservice.asmx ");

Then add the SOAP request data to postmethod.

Byte [] B = soaprequestdata. getbytes ("UTF-8 ");
Inputstream is = new bytearrayinputstream (B, 0, B. Length );
Requestentity Re = new inputstreamrequestentity (is, B. length, "application/soap + XML; charset = UTF-8 ");
Postmethod. setrequestentity (re );

 

Finally, an httpclient object is generated and a postmethod request is sent.

Httpclient = new httpclient ();
Statuscode = httpclient.exe cutemethod (postmethod );
String soaprequestdata = postmethod. getresponsebodyasstring ();

 

Soaprequestdata is the soap response data that calls the web service. It is in XML format and can be returned by parsing soaprequestdata.

2. Use xfire
JAR file xfire-all-1.2.4.jar, jdom-1.0.jar used
Method:
Define a client object and specify the Web Service's WSDL address

Client c = new client (new URL ("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx? (WSDL "));

 

Call the invoke method of the client object to specify the method name and parameters of the web service. The returned value is an object-type array.
The following code calls the getversiontime method. This method has no parameters, so the next parameter uses new object [0].

Object [] Results = C. Invoke ("getversiontime", new object [0]);

 

3. Use axis2
Download axis2-1.4
Method:
Open the console and go to the axis2-1.4/bin directory

What is the wsdl2java. Bat-Uri http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx? WSDL-P ws. clinet. axis2

After the preceding command is executed, a src directory is generated under the current directory. The xxxxcallbackhandler. Java and xxxxstub. java files are generated in the SRC \ ws \ clinet \ axis2 directory.
Wsdl2java generates the Web service call interface, parameter class, and return value Class Based on the WSDL file.
When WebService is called, a xxxxstub object is instantiated directly, and then the Web service method can be called.

4. Summary
The Web service set up by a tool may have a simpler call method corresponding to it, which is not described here. The preceding Method for calling Web Services is generic.
In the above three methods, httpclient should be flexible, but the development efficiency is low and difficult. xfire and axis2 are easy to use and the development speed is fast, but axis2 is not universal, some web services use axis2. Httpclient and xfire have good versatility. xfire is recommended for the above features.

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.