(轉)一個webservice的小demo

來源:互聯網
上載者:User

標籤:des   blog   http   io   os   使用   ar   for   檔案   

 .net平台內建了對Web Service的支援,包括Web Service的構建和使用。與其它開發平台不同,使用.net平台,你不需要其他的工具或者SDK就可以完成Web Service的開發了。.net Framework本身就全面支援Web Service,包括伺服器端的要求處理常式和對用戶端發送和接受SOAP訊息的支援。下來我們就一步一步的用Microsoft Visual Studio .net 2005(後面簡稱VS.Net 2005)建立和使用一個簡單的Web Service。 

    webservice,其實它就是個對外的介面,裡面有函數可供外部客戶調用(注意:裡面同樣有客戶不可調用的函數).假若我們是服務端,我們寫好了個webservice,然後把它給了客戶(同時我們給了他們調用規則),客戶就可以在從服務端擷取資訊時處於一個相對透明的狀態.即是客戶不瞭解(也不需要)其過程,他們只擷取資料.在代碼檔案裡,如果我們寫了一個函數後,希望此函數成為外部可調用的介面函數,我們必須在函數上面添上一行代碼[WebMethod(Description="函數的描述資訊")],如果你的函數沒有這個申明,它將不能被使用者引用.下來我們開始編寫一個簡單的Web Service 的例子.

2.1、用建立一個最簡單的Web Service 
首先,開啟VS2005,開啟“檔案-建立-網站”,選擇“ASP.NET Web服務”。 


在appcode/Service.cs中添加以下代碼 


 1using System; 
 2using System.Web; 
 3using System.Web.Services; 
 4using System.Web.Services.Protocols; 
 5[WebService(Namespace = "http://tempuri.org/")] 
 6[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
 7public class Service : System.Web.Services.WebService 
 8{ 
 9    public Service () { 
10        //如果使用設計的組件,請取消注釋以下行 
11        //InitializeComponent(); 
12    } 
13    //[WebMethod] 
14    //public string HelloWorld() { 
15    //    return "Hello World"; 
16    //} 
17    [WebMethod(Description = "求和的方法")] 
18    public double addition(double i, double j) 
19    { 
20        return i + j; 
21    } 
22    [WebMethod(Description = "求差的方法")] 
23    public double qiucha(double i, double j) 
24    { 
25        if (i > j) 
26            return i - j; 
27        else 
28            return j - i;                   
29    }     
30} 
31


2.調用webService裡的介面: 
1). 開啟VS2005,開啟“檔案-建立-網站”,選擇“ASP.NET網站”。 
2). 然後先添加Web引用,把WebService引到當前的工程裡面。方法是:在資源管理員中點擊右鍵,選擇添加Web 參考,調出對話方塊; 
3). 在URL中填入,前面寫好的WebService運行後瀏覽器上面顯示的地址,點擊“前往”按鈕,如,就會顯示出所引用的WebService中可以調用的方法,然後點擊“添加引用”,就將webservice引用到了當前的工程裡面 ,如,解決方案中會出現引進來的WebService檔案 
下面看代碼: 
default.aspx: 


 1<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 3<html xmlns="http://www.w3.org/1999/xhtml" > 
 4<head runat="server"> 
 5    <title>無標題頁</title> 
 6</head> 
 7<body> 
 8    <form id="form1" runat="server"> 
 9    <div> 
10        <asp:TextBox ID="Num1" runat="server"></asp:TextBox> 
11        <select id="selectOper" runat="server"> 
12            <option>+</option> 
13            <option>-</option> 
14         </select>          
15          <asp:TextBox ID="Num2" runat="server"></asp:TextBox> 
16           <span id = "E" runat = "server"></span> 
17           <asp:Button ID="btn" runat="server" Text="=" OnClick="btn_Click" />                 
18           <asp:TextBox ID="Result" runat="server"></asp:TextBox>         
19     
20    </div> 
21    </form> 
22</body> 
23</html> 
24


後台default.apsx.cs檔案: 


 1using System; 
 2using System.Data; 
 3using System.Configuration; 
 4using System.Web; 
 5using System.Web.Security; 
 6using System.Web.UI; 
 7using System.Web.UI.WebControls; 
 8using System.Web.UI.WebControls.WebParts; 
 9using System.Web.UI.HtmlControls; 
10
11public partial class _Default : System.Web.UI.Page 
12{ 
13    protected void Page_Load(object sender, EventArgs e) 
14    { 
15
16    } 
17    protected void btn_Click(object sender, EventArgs e) 
18    { 
19        if(Num1.Text!=null&&Num2.Text!=null) 
20        { 
21        localhost.Service objService=new localhost.Service(); 
22        int i = selectOper.SelectedIndex; 
23        switch (i) 
24        { 
25            case 0: 
26                Result.Text = objService.addition(double.Parse(Num1.Text),double.Parse(Num2.Text)).ToString(); 
27                break; 
28            case 1: 
29                Result.Text = objService.qiucha(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString(); 
30                break; 
31        } 
32        } 
33    } 
34} 
35


運行後可以看到效果,如所示,在前面兩個Textbox裡面輸入兩個運算元,在中間的下拉式清單中選擇操作符,然後點擊“=”號,將計算的結果輸出到第三個Textbox裡面。 

注意:而整個計算並不是在本地進行的,是在Web服務端進行計算的然後將結果通過XML返還給了調用方的,所以,在運行該程式的時候,WebService程式還必須啟動,否則會報無法串連遠程伺服器的異常. 
(參考自:http://www.cnblogs.com/zhangzheny/archive/2007/06/16/785734.html)

(轉)一個webservice的小demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.