Webservice publishing generally uses the style of the web service descriptive language file. In the WSDL file, this webservice is exposed to external interfaces for use. Today, we found a very good list of webservice providers.
Http://www.webservicex.net/WCF/default.aspx
More than 70 free webservice providers, utilities-> global weather, are listed above to obtain global weather forecasts.
The following shows how Java calls these web services through the WSDL file:
Note: The following code has not been tested. It only describes these situations. The Axis versions vary a lot. You 'd better refer to the example on the apache website, it is only used to describe its basic usage.
1. Direct AXIS calls a remote web service
I think this method is suitable for those experts who can directly understand the WSDL files in XML format. I cannot understand it myself, especially if I am not specialized in this line, even if I understand it for a while, later, I forgot. The direct call mode is as follows:
Import java. util. Date;
Import java. text. DateFormat;
Import org. apache. axis. client. Call;
Import org. apache. axis. client. Service;
Import javax. xml. namespace. QName;
Import java. lang. Integer;
Import javax. xml. rpc. ParameterMode;
Public class caClient {
Public static void main (String [] args ){
Try {
String endpoint = "http: // localhost: 8080/A3/services/caSynrochnized? Wsdl ";
// Directly reference a remote wsdl File
// The following are routines
Service service = new Service ();
Call call = (Call) service. createCall ();
Call. setTargetEndpointAddress (endpoint );
Call. setOperationName ("addUser"); // interface name described in the WSDL
Call. addParameter ("userName", org. apache. axis. encoding. XMLType. XSD_DATE,
Javax. xml. rpc. ParameterMode. IN); // Interface Parameters
Call. setReturnType (org. apache. axis. encoding. XMLType. XSD_STRING); // you can specify the return type.
String temp = "tester ";
String result = (String) call. invoke (new Object [] {temp });
// Pass parameters to the method and call the Method
System. out. println ("result is" + result );
}
Catch (Exception e ){
System. err. println (e. toString ());
}
}
}
2. Use SOAP to call a remote webservice.
I have never seen or tried this mode, but someone posted it on the Internet, and I also turned it over.
Import org. apache. soap. util. xml .*;
Import org. apache. soap .*;
Import org. apache. soap. rpc .*;
Import java. io .*;
Import java.net .*;
Import java. util. Vector;
Public class caService {
Public static String getService (String user ){
URL url = null;
Try {
Url = new URL ("http: // 192.168.0.100: 8080/A3/services/caSynrochnized ");
} Catch (MalformedURLException mue ){
Return mue. getMessage ();
}
// This is the main SOAP object
Call soapCall = new Call ();
// Use SOAP encoding
SoapCall. setEncodingStyleURI (Constants. NS_URI_SOAP_ENC );
// This is the remote object we're asking for the price
SoapCall. setTargetObjectURI ("urn: xmethods-caSynrochnized ");
// This is the name of the method on the above object
SoapCall. setMethodName ("getUser ");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector ();
// Name, type, value, encoding style
Parameter isbnParam = new Parameter ("userName", String. class, user, null );
SoapParams. addElement (isbnParam );
SoapCall. setParams (soapParams );
Try {
// Invoke the remote method on the object
Response soapResponse = soapCall. invoke (url ,"");
// Check to see if there is an error, return "N/"
If (soapResponse. generatedFault ()){
Fault fault = soapResponse. getFault ();
String f = fault. getFaultString ();
Return f;
} Else {
// Read result
Parameter soapResult = soapResponse. getReturnValue ();
// Get a string from the result
Return soapResult. getValue (). toString ();
}
} Catch (SOAPException se ){
Return se. getMessage ();
}
}
}
3. Use wsdl2java to convert the WSDL file into a local class and use it like a local class.
This is a favorite method for lazy people like me. We still use the previous global weather report as an example.
Java org. apache. axis. wsdl. WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL first
The original web site is http://www.webservicex.net/globalweather.asmx? WSDL, the question mark in the middle, but it cannot be parsed in Linux, so remove the question mark and change it to the point number.
Then four files will appear:
GlobalWeather. java GlobalWeatherLocator. java GlobalWeatherSoap. java GlobalWeatherSoapStub. java
GlobalWeatherSoap. java is the interface file we are most concerned about. If you are not interested in the details of RMI and other SOAP implementations, you only need to check the interface file, introduce this interface, just like using a local class.