【www.gdgbn.com--WebService】
webservice的调用,常用的大约有3种方式: 1、使用axis调用 2、使用xfire调用 3、使用cxf调用 项目中,采用axis进行调用,记录如下,备忘: ps教程:想当年的时候是用的xfire方式调用的,结果没做记录,现在已经完全记不得怎么玩了。所以说要多写博客啊 t_t 版本说明: aixs版本:axis-bin-1_4.zip java环境略 第一步:确保wsdl文件可用,文中为获取到sendsmsservice.wsdl,当然url的也行。 第二步:执行生成客户端代码的脚本。脚本内容为: set axis_lib=d:axis-1_4lib set java_cmd=java -djava.ext.dirs=%axis_lib% set output_path=. set package=info.jyzh.wap.liaoning.push %java_cmd% org.apache.axis.wsdl.wsdl2java sendsmsservice.wsdl -o%output_path% -p%package% -t #查看wsdl2java的使用帮助#%java_cmd% org.apache.axis.wsdl.wsdl2java -help ok,至此,客户端代码就生成出来了。还带了一个单元测试哦。 实际工作中,碰到以下情况,客户端不能直接连上webservice服务器,中间被强大的代理服务器挡住了。如下图: 为此,修改生成的代码,本次是在sendmmsserviceimplservicesoapbindingstub中作修改,如下: static { axisproperties.setproperty("http.proxyhost","88.88.88.88"); axisproperties.setproperty("http.proxyport","8080"); axisproperties.setproperty("http.proxyuser","asp教程yy"); axisproperties.setproperty("http.proxypassword","123456"); _operations = new org.apache.axis.description.operationdesc[1]; _initoperationdesc1();}
直接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文件转成本地类,然后像本地类一样使用,即可。