java调用.net Webservice

一、.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结束");
    }
}

你可能感兴趣的:(java,.net,webservice,String,service,SOAP)