1、用VS2005建立WebSevice
在VS中,新建“网站”,选择“ASP.net Web服务”
修改Service.cs代码:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
[WebService(Namespace = "http://tempuri.org/")]
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
//文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/200863/121183.html
public class Service : System.Web.Services.WebService
{
public Service () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World 大家好";
}
[WebMethod]
[SoapRpcMethod(Action = "show", RequestNamespace = "http://tempuri.org/")]
public string show(string type, string name, int ID)
{
return "/n 收到数据: 类别为:" + type + "/n 名称为: " + name + "/n ID为: " + ID.ToString();
}
[WebMethod]
public int ADD(int a, int b)
{
return a + b;
}
}
这样,WebSevice已经建好,可以直接按F5运行,也可以“生成”-“发布网站”,将功WebSevice发布到IIS中使用。
2、JAVA程序,使用axis访问
public static void show() {
try {
String endpoint = "http://localhost:8888/Service.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("http://tempuri.org/", "show"));
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/show");
// 该方法需要的参数
call.addParameter("type", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("name", org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("ID", org.apache.axis.encoding.XMLType.XSD_INT,javax.xml.rpc.ParameterMode.IN);
// 方法的返回值类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
String s = "水库";
String name = "大小";
int ID = 29;
String res = (String)call.invoke(
new Object[]{
s,name,ID
}
);
System.out.println( "Result: " + res);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public static void addTest() {
try {
Integer i = 1;
Integer j = 2;
//WebService URL
String service_url = "http://localhost:1902/donet_web/Service.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(service_url));
//设置要调用的方法
call.setOperationName(new QName("http://tempuri.org/", "ADD"));
//该方法需要的参数
call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);
//方法的返回值类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/ADD");
//调用该方法
Integer res = (Integer)call.invoke(
new Object[]{
i, j
}
);
System.out.println( "Result: " + res.toString());
} catch (Exception e) {
System.err.println(e);
}
}
运行,测试通过,可以很方便的用JAVA代码调用C#编写的WebSevice
几个注意事项:
WebSevice中,调用带参数的方法必须要加:[WebMethod]
[SoapRpcMethod(Action = "show", RequestNamespace = "http://tempuri.org/")]
不然C#的程序可以正常调用,JAVA的程序不能正常调用