%java_cmd% org.apache.axis.wsdl.wsdl2java sendsmsservice.wsdl -o%output_path% -p%package% -t
}
直接axis調用遠端web service
我覺得這種方法比較適合那些高手,他們能直接看懂xml格式的wsdl檔案,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,後來也就忘記了。直接調用模式如下:
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);//介面的參數
call.setreturntype(org.apache.axis.encoding.xmltype.xsd_string);//設定傳回型別
string temp = "測試人員";
string result = (string)call.invoke(new object[]{temp});
//給方法傳遞參數,並且調用方法
system.out.println("result is "+result);
}
catch (exception e) {
system.err.println(e.tostring());
}
}
}
2,直接soap調用遠端webservice
這種模式我從來沒有見過,也沒有試過,但是網路上有人貼出來,我也轉過來
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();
}
}
}
3,使用wsdl2java把wsdl檔案轉成本地類,然後像本地類一樣使用,即可。