您的位置:首页 > 编程语言 > Java开发

java调用.net Webservice

2009-10-27 10:23 423 查看
一、.net Server端

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

using System.Web.Services.Description;

namespace WebApplication1

{

    /// <summary>

    /// Example1 的摘要说明

    /// </summary>

    /*[WebService(Namespace = "urn:www.routon.com.cn")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]*/

    [WebService(Namespace = "http://www.haituns.com")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

    // [System.Web.Script.Services.ScriptService]

    public class Example1 : System.Web.Services.WebService

    {

       /*[SoapDocumentMethodAttribute(

            Action = "",

            RequestNamespace = "urn:www.routon.com.cn",

            RequestElementName = "sayHello",

            ResponseNamespace = "urn:www.routon.com.cn",

            ResponseElementName = "arithmeticMeanResponse",

            Use = SoapBindingUse.Encoded)

        ]*/

        [SoapRpcMethod(Use = SoapBindingUse.Literal, RequestNamespace = "http://www.haituns.com", ResponseNamespace = "http://www.haituns.com")]

        [WebMethod]

        public string sayHello(string name)

        {

            return "Hello " + name;

        }

    }

}

 

二、java Client端

需要引入axis.jar

commons-discovery-0.2.jar

commons-logging-1.0.4.jar

jaxrpc.jar

log4j-1.2.8.jar

mail.jar

saaj.jar

wsdl4j-1.5.1.jar

 

import java.net.MalformedURLException;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

public class Main {

    public static void main(String args[]) {

        System.out.println("测试SOAP开始");

        Service service = new Service();

        Call call = null;

        try {

            call = (Call) service.createCall();

        } catch (ServiceException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        String name = "good!";

//        String endpoint = "http://www.haituns.net/test/hello.asmx";

        String endpoint = "http://127.0.0.1:2370/Example1.asmx";

        String nameSpace = "http://www.haituns.com";

//        String nameSpace = "http://CAIE.EAI.ExpenseConstract.WebServices";

        try {

            call.setTargetEndpointAddress(new java.net.URL(endpoint));

        } catch (MalformedURLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        call.setOperationName(new QName(nameSpace, "sayHello"));

//        call.addParameter(new QName(nameSpace, "name"),

//                org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);

        call.addParameter("name", org.apache.axis.encoding.XMLType.XSD_STRING,ParameterMode.IN);

        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

        call.setUseSOAPAction(true);

        call.setSOAPActionURI(nameSpace+"/sayHello");

        String res = "";

        try {

            res = (String) call.invoke(new Object[] {name});

        } catch (RemoteException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        System.out.println("返回值=" + res);

        System.out.println("测试SOAP结束");

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息