Java與.NET的WebServices相互調用

來源:互聯網
上載者:User

標籤:

一:簡介 
本文介紹了Java與.NET開發的Web Services相互調用的技術。本文包括兩個部分,第一部分介紹了如何用.NET做用戶端調用Java寫的Web Services,第二部分介紹了如何用Java做用戶端調用.NET開發的Web Services。  
二:項目需要的工具  
WindowsXP Professial(IIS)

Eclipse3.1(已裝好Tomcat axis)VS.Net 2003備忘:下載安裝Tomcat5.5.16 以及 Axis的開發包,並做相應配置。看我前一片文檔。三:用.NET做用戶端調用Java寫的Web Services 
1.產生一個Java Web Services
使用Eclipse產生一個Web Services是非常簡單的。建立一個Web工程,右鍵建立一個Class,名為HelloClient.java。代碼如下:java 代碼:public class HelloClient    
{    
public String getName(String name)    
{    
return "Hello,"+name;    
}    
}   

右鍵此檔案選擇"WebServices"-->"Create Web Service"即可部署為原生一個ws。在IE瀏覽器中輸入http://localhost:8008/testWebService/services/HelloClient可以看到已經部署了的HelloClient服務。輸入http://localhost:8008/testWebService/services/HelloClient?wsdl這個服務的描述檔案。

     建立一個項目(WinForm,ASP.net都可以),我在這裡使用的是一個Asp.Net項目。在起始頁面上放置一個文本輸入框用來輸入,一個標籤用來顯示調用Web Services的結果,放置一個按鈕,用來單擊調用Web Services。然後,選擇添加Web 參考,URL一欄裡填入http://localhost:8008/testWebService/services/HelloClient?wsdl,Web 參考的名稱輸入JavaWebService,單擊添加引用按鈕就可以了。此時,我們可以在VS.net 的Solution Explore中看到這個Web 參考。
添加代碼如下:

c# 代碼:protected System.Web.UI.WebControls.TextBox txtName;    
protected System.Web.UI.WebControls.Button btnSend;    
protected System.Web.UI.WebControls.Label lblMsg;    
   
private JavaWebService.HelloClientService ws;    
private void Page_Load(object sender, System.EventArgs e)    
{    
// 在此處放置使用者代碼以初始化頁面    
ws=new testWebService.JavaWebService.HelloClientService();    
}    
   
private void btnSend_Click(object sender, System.EventArgs e)    
{    
lblMsg.Text=ws.getName(txtName.Text);    
}   

 

這樣,一個.NET用戶端就完成了,測試一下,工作正常,OK.

四:用Java做用戶端調用.NET寫的 Web Services

1. 用VS.NET建立一個Asp Web Services工程,添加一個web 服務,命名為Service1.asmx。新增一個web method,代碼如下:

c# 代碼:
//指定SoapAction    
[SoapRpcMethod(Action="http://www.zjh.com/Rpc",RequestNamespace="http://www.zjh.com/SU",ResponseNamespace="http://www.zjh.com/SU")]    
[WebMethod]    
public string getName(string name)    
{    
return "Hello"+name;    
}  

 

然後運行它,並利用IE進行測試成功。

2. 開啟Eclipse,建立一個Web項目,添加一個JSP頁面 ,命名為action_hello.jsp,輸入下列代碼:

java 代碼:
<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>    
<%@page import="org.apache.axis.client.Call"%>    
<%@page import="org.apache.axis.client.Service"%>    
<%@page import="javax.xml.namespace.QName"%>    
java 代碼
<%    
request.setCharacterEncoding("GBK");    
String name=request.getParameter("name");    
String endpoint="http://localhost/HelloClient/Service1.asmx";    
Service service = new Service();    
Call call = (Call)service.createCall();    
call.setTargetEndpointAddress(new java.net.URL(endpoint));    
call.setOperationName(new QName("http://www.zjh.com/SU","getName"));    
call.addParameter("name",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);    
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);    
call.setUseSOAPAction(true);    
call.setSOAPActionURI("http://www.zjh.com/Rpc");    
String res =(String)call.invoke(new Object[]{name});    
out.print(res);    
%>    

 

這個要比Java調用Java寫的ws來的複雜。ws和Java用戶端都要設定SOAPAction,否則會有異常說 System.Web.Services.Protocols.SoapException: 伺服器未能識別 HTTP 標題 SOAPAction 的值


五:總結
經過兩天的實驗,終於從技術上明確了通過Web Services實現.net 與 java的互連是可能的。當然有幾點是需要注意的:

  1. 在提供Web Services的時候,盡量使用xml schema中支援的變數類型做參數。如果使用.net 中的dataset這種類型,對於java來說解析起來將是一個災難,當然,理論上是可以解析的。但是從效率角度來說,在Web Services與用戶端交換資訊的過程中,始終有一個序列化和還原序列化的問題。如果使用dataset這種類型,系統還需要對它進行序列化操作,這將是一個很耗費資源的過程。而使用string類型將簡單很多。

  2. 如果使用了soap header等擴充功能,例如使用了微軟提供的WSE技術,它們之間的相互連信需要作特殊處理。

 引自:http://www.cnblogs.com/lzhdim/archive/2008/12/21/1359308.html

 

Java與.NET的WebServices相互調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.