webservice调用接口,webservice的接口

http://www.itjxue.com  2023-01-20 00:44  来源:未知  点击次数: 

如何调用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接口怎么使用

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接口

在项目中选择【控制台应用程序】,点击项目右键,选择添加-服务引用。在地址栏中输入WebServie链接地址后回车,点击确定后在代码中就可以看到添加的服务应用了,详细步骤:

1、首先打开VS2013,选择文件-新建-项目。

2、在项目中选择【控制台应用程序】,将项目名称重新命名为【WebServiceTest】。

3、点击项目右键,选择添加-服务引用。

4、在地址栏中输入WebServie链接地址后回车,然后重新命名服务名称为【ServiceGetWeather】,点击确定后在代码中就可以看到添加的服务应用了。

5、然后在代码中添加如下代码,调用webservice,获取接口返回的数据,呈现出来。

6、这是运行的结果,可以看出接口返回了一个xml格式的数据。

java如何调用webservice接口?

Java通过WSDL文件来调用webservice直接调用模式如下:

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());

}

}

}

如何调用webservice接口

webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。今天搜索到了非常好的 webservice provider列表

这上面列出了70多个包括很多方面的free webservice provider,utilities-global weather就可以获取全球的天气预报。

下面我们来看Java如何通过WSDL文件来调用这些web service:

注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。

1,直接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文件转成本地类,然后像本地类一样使用,即可。

这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。

首先 java org.apache.axis.wsdl.WSDL2Java

原本的网址是,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。

那么就会出现4个文件:

GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java

其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候,引入这个接口即可,就好像使用本地类一样。

webservice接口是什么?它有什么作用?

webService接口是一种常用的短信群发提交接口,使用时可以象调用一般函数一样调用WebService的方法。

作用是:该接口主要用于客户自身的软件、网站或其他产品,需要将短信群发功能集成于软件、网站内作为产品整体功能的一部分,在这种情况下可以使用WebService接口来提交短信。实现WebService接口的短信群发功能需要有一定的基本编程能力。如VC++、Delphi、ASP等。

扩展资料:

WebService接口技术支持

1、XML

可扩展的标记语言(标准通用标记语言下的一个子集)为Web Service平台中表示数据的基本格式。XML是由万维网协会(W3C)创建,W3C制定的XML SchemaXSD定义了一套标准的数据类型,并给出了一种语言来扩展这套数据类型。

2、SOAP

SOAP用于交换XML(标准通用标记语言下的一个子集)编码信息的轻量级协议。它有三个主要方面:XML-envelope为描述信息内容和如何处理内容定义了框架,将程序对象编码成为XML对象的规则,执行远程过程调用(RPC)的约定。SOAP可以运行在任何其他传输协议上。

3、WSDL

Web Service描述语言WSDL,就用机器能阅读的方式提供的一个正式描述文档而基于XML(标准通用标记语言下的一个子集)的语言,用于描述Web Service及其函数、参数和返回值。

参考资料来源:百度百科-短信平台接口

参考资料来源:百度百科-webService

(责任编辑:IT教学网)

更多

推荐网页文字特效文章