webservice使用,webservice

http://www.itjxue.com  2023-01-14 16:22  来源:未知  点击次数: 

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 = ""; //直接引用远程的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(""); } 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文件转成本地类,然后像本地类一样使用,即可。

webservice测试工具怎么用

一、通过WSCaller.jar工具进行测试:

前提:知道wsdl的url。

wsCaller可执行程序的发布方式为一个wsCaller.jar包,不包含Java运行环境。你可以把wsCaller.jar复制到任何安装了Java运行环境(要求安装JRE/JDK 1.3.1或更高版本)的计算机中,用以下命令运行wsCaller:

java -jar wsCaller.jar

使用wsCaller软件的方法非常简单,下面是wsCaller的主界面:

首先在WSDL Location输入框中输入你想调用或想测试的Web

Service的WSDL位置,,然后点“Find”按钮。wsCaller就会检查你输入的URL地址,并获取Web

Service的WSDL信息。如果信息获取成功,wsCaller会在Service和Operation下拉列表框中列出该位置提供的Web

Service服务和服务中的所有可调用的方法。你可以在列表框中选择你要调用或测试的方法名称,选定后,wsCaller窗口中间的参数列表框就会列出

该方法的所有参数,包括每个参数的名称、类型和参数值的输入框(只对[IN]或[IN,

OUT]型的参数提供输入框)。你可以输入每个参数的取值。如下图:

这时,如果你想调用该方法并查看其结果的话,只要点下面的“Invoke”按钮就可以了。如果你想测试该方法的执行时间,则可以在“Invoke

Times”框中指定重复调用的次数,然后再按“Invoke”按钮。wsCaller会自动调用你指定的方法,如果调用成功,wsCaller会显示结

果对话框,其中包括调用该方法所花的总时间,每次调用的平均时间和该方法的返回值(包括返回值和所有输出型的参数)。如下图:

wsCaller软件是基于Axis库(Apache eXtensible Interaction System)开发的,Axis库的介绍及其版权信息请参见Apache Software Foundation的网站。

二、编写java的Main()函数,创建XFireProxyFactory调用声明的Interface接口函数。

前提:知道wsdl的url、知道webservice程序的接口(方法)声明。

举例:

public static void main(String[] args) {

Service srvcModel = new ObjectServiceFactory()

.create(IHelloWorldService.class);

XFireProxyFactory factory = new XFireProxyFactory(XFireFactory

.newInstance().getXFire());

String helloWorldURL = "";

try {

IHelloWorldService srvc = (IHelloWorldService) factory.create(

srvcModel, helloWorldURL);

System.out.print(srvc.example("dennis"));

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

三、利用Eclipse向导生成webservice的客户端调用程序。

前提:知道wsdl的url、知道webservice程序的接口(方法)声明。

步骤:

1)在Eclipse中加入Xfire插件:

在Eclipse的安装目录的plugins下包含:

org.codehaus.xfire.eclipse.ui_1.0.2.xfire126.jar、

org.codehaus.xfire.eclipse.ui.wizards_1.0.2.xfire126.jar、

org.codehaus.xfire.eclipse.core_1.0.2.xfire126.jar

在Eclipse的安装目录的features下包含:

org.codehaus.xfire.eclipse_1.0.2.xfire126

2) 在Eclipse中创建一个Web Project项目,设为WSClient项目名。

同时点击项目右键,添加Xfire nature。

3)在WSClient项目下创建一个Web Service Client文件组【或者是Code Generation from WSDL document的文件组!】。

即点击项目右键,new--》other--》...

输入webservice的url,比如:

再选好生成文件放置目录。完成生成!

4)修改所生成文件组中的文件:****client.java 中的main函数.

比如:生成的文件为BizRemoteServiceClient.java,在main函数中找到:

BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoapLocalEndpoint();

该语句用BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoap();代替即可!

public static void main(String[] args) {

BizRemoteServiceClient client = new BizRemoteServiceClient();

//create a default service endpoint

//BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoapLocalEndpoint();

BizRemoteServiceSoap bizRemoteServiceSoap = client.getBizRemoteServiceSoap();

//TODO: Add custom client code here

//

//bizRemoteServiceSoap.yourServiceOperationHere();

MyResponse MyResponse=bizRemoteServiceSoap.login("ivy1", "111");

System.out.println(MyResponse.getFlag());

System.out.println(MyResponse.getDetail());

System.out.println("test client completed");

System.exit(0);

}

绿色代码部分是根据测试业务逻辑加入的测试代码!试具体测试任务而定!

直接运行该main函数,看结果即可!

webservice怎么使用

通过添加WEB引用的方式调用,首先添加WEB引用,通过URL指向WEBSERVICE,

指定WEB引用名,假设为KK;

示例:

kk.WebService n = new kk.WebService();

string ss=n.HelloWorld();

二、WebService在前台页面的JS 调用方法

1、首先通过下面的方法把Webservice在前台引用进来

asp:ScriptManager runat="server"

Services

asp:ServiceReference Path="WebService.asmx" InlineScript="True" /

/Services

/asp:ScriptManager

2、然后就可以通过JS程序进行调用,示例如下:

script type="text/jscript"

function a()

{

WebService.HelloWorld(onresult);

}

//这里的onresult是回调函数

function onresult(result)

{

alert(result);

}

function b()

{

WebService.add(1,2,onreturn)

}

function onreturn(result)

{

alert(result);

}

//下面的'context'是上下文,可以通过回到函数通过重载的方式获得;

function c()

{

WebService.div(1,1,onresultC,onerror,'context');

}

function onresultC(res,c)

{

alert(res);

alert(c);

}

//onerror是获得异常信息的回调函数,下面给出了获得异常信息的方法

function onerror(error)

{

var a="";

a=String.format("获取服务器端异常的具体类型:{0}\t\n获取详细的异常描述信息:{1}\t\n获取造成异常的:{2}\t\n获取服务器端异常的堆栈

跟踪信息:{3}\t\n获取一个布尔值,表示异常是否是由于网络连接超时造成的{4}",

error.get_exceptionType(),

error.get_message(),

error.get_statusCode(),

error.get_stackTrace(),

error.get_timedOut())

alert(a);

}

a();

b();

c();

/script

(责任编辑:IT教学网)

更多

推荐PHP+MySQL视频文章