Java SOAP 調用 C# 的WebService

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());
        }
    }

    

}

測試運行

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章