Java如何通过WSDL文件来调用这些web service
生活随笔
收集整理的這篇文章主要介紹了
Java如何通过WSDL文件来调用这些web service
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
下面我們來看Java如何通過WSDL文件來調(diào)用這些web service:
注意,以下的代碼并沒有經(jīng)過真正的測試,只是說明這些情況,不同版本的Axis相差很大,大家最好以apache網(wǎng)站上的例子為準,這里僅僅用于說明其基本用法。?
1,直接AXIS調(diào)用遠程的web service
我覺得這種方法比較適合那些高手,他們能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,后來也就忘記了。直接調(diào)用模式如下:
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/ca3/services/caSynrochnized?wsdl";
//直接引用遠程的wsdl文件
//以下都是套路?
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名稱
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的參數(shù)
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設(shè)置返回類型?
String temp = "測試人員";?
String result = (String)call.invoke(new Object[]{temp});
//給方法傳遞參數(shù),并且調(diào)用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP調(diào)用遠程的webservice
這種模式我從來沒有見過,也沒有試過,但是網(wǎng)絡(luò)上有人貼出來,我也轉(zhuǎn)過來?
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/ca3/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/A"
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();
}
}
}
注意,以下的代碼并沒有經(jīng)過真正的測試,只是說明這些情況,不同版本的Axis相差很大,大家最好以apache網(wǎng)站上的例子為準,這里僅僅用于說明其基本用法。?
1,直接AXIS調(diào)用遠程的web service
我覺得這種方法比較適合那些高手,他們能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,后來也就忘記了。直接調(diào)用模式如下:
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/ca3/services/caSynrochnized?wsdl";
//直接引用遠程的wsdl文件
//以下都是套路?
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名稱
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的參數(shù)
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//設(shè)置返回類型?
String temp = "測試人員";?
String result = (String)call.invoke(new Object[]{temp});
//給方法傳遞參數(shù),并且調(diào)用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP調(diào)用遠程的webservice
這種模式我從來沒有見過,也沒有試過,但是網(wǎng)絡(luò)上有人貼出來,我也轉(zhuǎn)過來?
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/ca3/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/A"
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();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的Java如何通过WSDL文件来调用这些web service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Java调用以WSDL形式发布的we
- 下一篇: WSDL 详解