●使用.NET建立Web Services

1. 開啟.Net,建立「ASP.NET Web Services應用程式」專案。

2. 範例程式碼如下:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function printWord(ByVal strTmp As String) As String
        Return strTmp
    End Function

End Class

需把web.config中的<add name="ScriptModule" ...>給註解掉。

3. 看到如下的畫面就表示成功了!比起Java,用.NET只能說是小菜一碟。

ws01.png

 ws02.png

●使用.NET建立Web Services Client呼叫.NET建立的Web Services

在「方案總管」中的「Web References」按右鍵選取「加入Web參考」,再選擇要用哪一個Web Services,可直接鍵入asmx的網址。

ws3.png

ws4.png

亦需把web.config中的<add name="ScriptModule" ...>註解掉

修改程式碼,此範例為直接給Web services值,再顯示在畫面上。

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ws As New ws1.Service1
        Dim return_answer As String = ws.printWord("Miles")
        TextBox1.Text = return_answer
    End Sub
End Class

●使用Java建立Web Services Client呼叫.NET建立的Web Services

新增一Dynamic Project,wsclient。在wsclient專案上按右鍵新增Web Service Client。

在Service definition輸入WSDL的網址。

  ws5.png

 ws6.png

在wsclient專案下新增一檔案ws.java

 

package org.tempuri;

public class ws {

 public static void main(String[] args) {
  Service1SoapProxy sp = new Service1SoapProxy();
  try {
   System.out.println("WS Result: "+sp.printWord("Miles"));
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }

}

恭喜大功告成!

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 miles0722 的頭像
    miles0722

    文言文

    miles0722 發表在 痞客邦 留言(0) 人氣()