四:用Java做客户端调用.NET写的 Web Services
有了上面的成功,使我以为用Java做客户端调用也是一件十分容易的事情,可实际情况却耗费了我两天时间才得以实现。
1. 用VS.NET新建一个Asp Web Services工程,添加一个web 服务,命名为SumService.asmx。新增一个web method,代码如下:
[WebMethod]
public int IntAdd(int a,int b)
{
return a+b ;
}
public int IntAdd(int a,int b)
{
return a+b ;
}
然后运行它,并利用IE进行测试成功。
2. 打开jbuilder9.0,新建一个项目,添加一个java class ,命名为TestNetService,输入下列代码:
package MyWebServiceJavaClient;
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;
/**
*
*
*
*
* @author not attributable
* @version 1.0
*/
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/MyServices/WebServiceTest/SumService.asmx";
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","IntAdd"));
call.addParameter("a",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);
call.addParameter("b",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);
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());}
}
}
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;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2004
*
Company:
* @author not attributable
* @version 1.0
*/
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/MyServices/WebServiceTest/SumService.asmx";
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","IntAdd"));
call.addParameter("a",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);
call.addParameter("b",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);
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());}
}
}
