Java SOAP 调用 C# 的WebService

发布时间 2023-10-23 21:21:20作者: 龙骑科技

Java SOAP 调用 C# 的WebService,C# 的WebService方法的创建可以参考上一篇文章

IntelliJ IDEA Community Edition 2021.2.3的idea64.exe新建项目,导入需要的jar,代码如下:

import org.apache.axis.client.Service;
import org.apache.axis.soap.SOAPConstants;
import javax.xml.namespace.QName;
import java.net.URL;

public class Longteng {

    public static void main(String[] args) {
        TestWebserviceHelloWorld3();
    }

    public static void TestWebserviceHelloWorld3() {
        try {
            //String url = "http://localhost:8012/WebService1.asmx";
            //String namespace = "http://tempuri.org/";
            Service service = new Service();
            Call call=(Call)(new Service()).createCall();
            call.setTargetEndpointAddress(new URL("http://localhost:8012/WebService1.asmx"));
            call.setOperationName(new QName("http://tempuri.org/","HelloWorld3"));
            call.setTimeout(30000);
            call.setUseSOAPAction(true);
            call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
            //解决 提示System.Web.Services.Protocols.SoapException: 服务器未能识别 HTTP 头 SOAPAction 的值: 。的错误问题
            // SOAP 1.1 必须添加SOAPAction,SOAP 1.2 则不能添加SOAPAction
            call.setSOAPActionURI("http://tempuri.org/HelloWorld3");
            String re=(String)call.invoke(new String[]{"1","pass"});
            System.out.println(re);

            //Call call=(Call)(new Service()).createCall();
            //call.setTargetEndpointAddress(new URL("http://127.0.0.1:7001/hnCardService/services/CardService"));
            //call.setOperationName(new QName("http://ws.apache.org/axis2","getCard"));
            //call.setTimeout(30000);
            //String re=(String)call.invoke(new String[]{"user","pass","F20000015","420503198104191819","周玉磊","440200"});
            //System.out.println(re);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

    

}

测试运行