开发,调用WebService
public class Calculator {
public int add(int i,int j)
{
return i+j;
}
public int substract(int i,int j)
{
return i-j;
}
1. 放在axis下,更名为Calculator.jws
2. 利用axis生成WSDL,并复制IE中的URL,下面将用到。
3. 调用web服务
1) 用.net调用
新建一个项目(WinForm,ASP.net都可以)。在起始页面上放置一个文本输入框用来显示调用Web Services的结果,放置一个按钮,用来单击调用Web Services。然后,选择添加Web 引用,在WSDL一栏中把刚才得到的WSDL地址复制过来,Web 引用的名称输入JavaService,单击添加引用按钮就可以了。此时,我们可以在VS.net 的Solution Explore中看到这个Web 引用。
在按钮的单击事件中输入下列代码:
int i = int.Parse(textBox2.Text.ToString());
int j = int.Parse(textBox3.Text.ToString());
textBox1.Text = js.add(i,j).ToString();
其中JavaService是引用Web服务是自己定义的名字,如下图:
CalculatorService是wsdl中Service的name,如下图:
1) 用java调用Wenservice
a) 首先新建一个类(TestNetService.Java)
import java.util.Date;
import java.text.DateFormat;
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 TestNetService {
public TestNetService() {
}
public static void main(String[] args) {
try {
Integer i = new Integer(1);
Integer j = new Integer(2);
String endpoint="http://localhost:8080/axis/Calculator.jws";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setoperationName(new QName("http://www.my.com/SU","add"));
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://www.my.com/Rpc");
Integer k = (Integer)call.invoke(new Object[]{i,j});
System.out.println( "result is " + k.toString() + ".");
}
catch (Exception e) {System.err.println(e.toString());}
}
d) 注意:
call.setoperationName(new QName("http://www.my.com/SU","add"));中的add是应用程序中的函数名,或者说是WSDL中operation的name的值。
@H_404_577@
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。