java呼叫.net的webservice
阿新 • • 發佈:2019-01-07
一.參考文獻
二.概述
前面寫了一篇部落格eclipse+webservice 是在java環境下進行的。考慮到webservice的跨系統,跨語言,跨網路的特性,下面寫了一個例項來測試其跨語言的的特性。
首先是用asp.net開發一個webservice,然後再java中建立客戶端來呼叫這個service。
三.例項
(1)開啟visual studio 2010,新建專案,如下圖所示:
新建的專案結果如下圖所示:
(2)在Service1.asmx.cs中新增服務方法,程式碼如下:
(3)釋出服務,按CTRL+F5執行專案,即可開啟服務首頁:http://localhost:5329/Service1.asmx,如下圖所示:using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace AspWebService1 { /// <summary> /// Service1 的摘要說明 /// </summary> [WebService(Namespace = "http://erplab.sjtu.edu/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從指令碼中呼叫此 Web 服務,請取消對下行的註釋。 // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string sayHelloToPersonNew(String name) { if (name == null) { name = "nobody"; } return "hello," + name; } [WebMethod] public double count(double number, double price, double discount) { return number * price * discount; } [WebMethod] public float getFloat(float x) { return x; } //加法 [WebMethod] public float plus(float x, float y) { return x + y; } //減法 [WebMethod] public float minus(float x, float y) { return x - y; } //乘法 [WebMethod] public float multiply(float x, float y) { return x * y; } //除法 [WebMethod] public float divide(float x, float y) { if (y != 0) { return x / y; } else return -1; } } }
上圖中顯示的就是我們在Service1.asmx.cs檔案中定義的服務方法。點選“服務說明”可以檢視webservice的wsdl檔案。
(4)編寫java客戶端來測試webservice,java程式如下所示:
執行結果:package edu.sjtu.erplab.aspwebservice; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public class AspWebServiceTestClient1 { public static void main(String[] args) throws Exception { // 定義方法 String method = "HelloWorld"; String methodPlus = "plus"; String methodMinus = "minus"; String methodMultiply = "multiply"; String methodDivide = "divide"; String methodSayTo = "sayHelloToPersonNew"; // 定義服務 Service service = new Service(); // 測試1:呼叫HelloWorld方法,方法沒有引數 Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call.setUseSOAPAction(true); // 第一種設定返回值型別為String的方法 call.setReturnType(XMLType.SOAP_STRING); call.setOperationName(new QName("http://erplab.sjtu.edu/", method)); call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld"); String retVal1 = (String) call.invoke(new Object[] {}); System.out.println(retVal1); // 測試2: 呼叫sayHelloToPersonNew方法,方法有一個引數:name。sayHelloToPersonNew(String name) Call call2 = (Call) service.createCall(); call2.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call2.setUseSOAPAction(true); call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema", "string")); // 第二種設定返回值型別為String的方法 call2.setOperationName(new QName("http://erplab.sjtu.edu/", methodSayTo)); call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew"); call2.addParameter(new QName("http://erplab.sjtu.edu/", "name"),// 這裡的name對應引數名稱 XMLType.XSD_STRING, ParameterMode.IN); String retVal2 = (String) call2 .invoke(new Object[] { "asp webservice" }); System.out.println(retVal2); // 測試3: 呼叫sgetFloat方法,方法有一個引數:x,為float型別 Call call3 = (Call) service.createCall(); call3.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call3.setUseSOAPAction(true); call3.setEncodingStyle(null);// 必須有,否為會系統報錯。最關鍵的語句。決定生成xmlns的中soap的名稱空間 // 第一種設定返回值型別為Float型別的方法 call3.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT); call3.setOperationName(new QName("http://erplab.sjtu.edu/", "getFloat")); call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat"); call3.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 這裡的x對應引數名稱 XMLType.XSD_FLOAT, ParameterMode.INOUT); Float retVal3 = (Float) call3.invoke(new Object[] { 123 }); System.out.println(retVal3); // 測試4: 呼叫plus方法,方法有兩個引數:x,y。plus(float x, float y) Call call4 = (Call) service.createCall(); call4.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call4.setUseSOAPAction(true); call4.setEncodingStyle(null); // 第二種設定返回值型別為Float型別的方法 call4.setReturnType(new QName("http://www.w3.org/2001/XMLSchema", "float")); call4.setOperationName(new QName("http://erplab.sjtu.edu/", methodPlus));// 加法 call4.setSOAPActionURI("http://erplab.sjtu.edu/plus"); call4.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 引數x org.apache.axis.encoding.XMLType.XSD_FLOAT, ParameterMode.IN); call4.addParameter(new QName("http://erplab.sjtu.edu/", "y"),// 引數y XMLType.XSD_FLOAT, ParameterMode.IN); Float retVal4 = (Float) call4.invoke(new Object[] { 5, 6 }); System.out.println(retVal4); } }
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
Hello World
hello,asp webservice
123.0
11.0
注意點:
(a)我們發現如果引數是String型別的,那麼可以不需要設定call的引數 call3.setEncodingStyle(null); 但是如果傳入引數為float型別,那麼就必須加上這一條語句。
(b)設定返回值型別有兩種方式:
一種是
call.setReturnType(XMLType.SOAP_STRING);
另外一種是
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));
這兩種方法是等價的