天天看点

web service DEMO: C#提供web service,java进行调用asmx

1.C#的服务源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TestWebService01
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class HelloSerivce : System.Web.Services.WebService
    {
        private int age;
        private string name;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }


        [WebMethod]
        public string helloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public string showMyInfo(string name,int age)
        {
            this.Age = age;
            this.Name = name;

            return "My name is " + this.Name + ",my age is " + this.Age;
        }
    }
}
           

2.java的调用服务代码:

package javaRevokeNetService;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class testService {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			//注意:这里url应该填写vs中绑定的ip和动态端口
			String wsUrl = "http://localhost:30886/HelloSerivce.asmx";
			String nameSpace = "http://tempuri.org/";
			String opName = "showMyInfo";
			String SOAPActionURI = nameSpace + opName;
			//初始化调用服务的组件
			Service service = new Service();
			Call call = null;
			call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(wsUrl));
			call.setSOAPActionURI(SOAPActionURI);
			call.setOperationName(new QName(nameSpace, opName));

			String param1 = "name";
			String paramVal1 = "小天才";
			String param2 = "age";
			String paramVal2 = "23";
			String result = "";

			call.addParameter(new QName(nameSpace, param1), XMLType.XSD_STRING,ParameterMode.IN);
			call.addParameter(new QName(nameSpace, param2), XMLType.XSD_STRING,ParameterMode.IN);
			call.setReturnClass(String.class);
			//调用WebService
			result = (String) call.invoke(new Object[] { paramVal1, paramVal2 });
			System.out.print(result);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e);
		}

	}

}
           

3.联调的结果:

web service DEMO: C#提供web service,java进行调用asmx
web service DEMO: C#提供web service,java进行调用asmx

一个由C#发布的asmx服务和JAVA调用该服务的demo。简单易懂,可以直接运行进行联合调试。

一个C#发布服务和JAVA调用服务的demo.rar

(缺分,谢谢给一分。如有需要,可m本人直接发)

下载链接:点击打开链接

谢谢!