WebService開發應用


WebService是運行於服務端(一般放在信息服務器上的)讓客戶端來調用的。

以下開發兩個簡單的實例

1.自己開發服務端自己調用(vs2010)

   1).菜單:“新建-項目”,在打開的窗體中選擇,如下圖:

    2).在“項目解決方案”中右擊此項目並“添加-新建項”,然后選擇"web服務",如下圖

  3).打開新添加的頁面,在其中加入四個函數,一定在四個函數的上方加上“[WebMethod]”,這是說明讓客戶端來調用的函數,如果上面沒有或注釋掉,就表示客戶端不能訪問它。下面把它默認的HelloWord函數注釋,源碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace MyWebServices
{
    /// <summary>
    /// WebService1 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        //[WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod(Description="相加")]
        public double Add(double num1, double num2)
        {
            return num1 + num2;
        }

        [WebMethod(Description = "相減")]
        public double Sub(double num1, double num2)
        {
            return num1 - num2;
        }

        [WebMethod(Description = "相乘")]
        public double Mul(double num1, double num2)
        {
            return num1 * num2;
        }

        [WebMethod(Description = "相除")]
        public double Div(double num1, double num2)
        {
            if (num2 != 0)
                return num1 / num2;
            else
                return 0;
        }
    }
}

4).在瀏覽器中運行WebService1.asmx,即在“解決方案”中右鍵WebService1.asmx,在"瀏覽器中運行",以下為運行圖,會發現HelloWord函數沒顯示出來

5).開發客戶端。

  •     建立一空的WebApplication程序
  •     在“解決方案”中右鍵此項目“添加web引用”,會彈出一窗體,讓用戶輸入wsdl的URL.設置Web引用名的名稱並點擊添加引用,此時會在項目中出現一個這樣服務的圖標。如下圖:
  • 增加一webForm,它的HTML源碼為:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <input id="Text1" type="text" runat="Server" /><select id="Select1" name="D1" runat="Server" >
            <option>+</option>
            <option>-</option>
            <option>*</option>
            <option>/</option>
        </select><input id="Text2" type="text" runat="Server" /><asp:Button ID="Button1" 
            runat="server" Text="=" onclick="Button1_Click" />
        <input id="Text3" type="text" runat="Server" /></div>
    </form>
</body>
</html>

cs源碼(按鈕事件):

 protected void Button1_Click(object sender, EventArgs e)
        {
            string op = Select1.Value;
            if (Text1.Value == string.Empty || Text2.Value == string.Empty)
                return;
            double num1 = double.Parse(Text1.Value);
            double num2 = double.Parse(Text2.Value);
            double result=0;
            MyTest.WebService1 ws=new MyTest.WebService1();
            if (op.Equals("+"))
                result = ws.Add(num1, num2);
            else if (op.Equals("-"))
                result = ws.Sub(num1, num2);
            else if (op.Equals("*"))
                result = ws.Mul(num1, num2);
            else if (op.Equals("/"))
                result = ws.Div(num1, num2);
            Text3.Value = result.ToString();

        }
  • 運行客戶端,如下圖,此時成功運行:

  2.調用其它的WebService服務,此例我們調用http://www.webxml.com.cn中的查詢手機號碼的服務,打開此網站下的"全部WebService",可以看到如下圖:

      1).新建 一個普通的WinForm程序,界面如下:

     

   2).在新建的項目上右鍵"添加服務引用",在地址欄上粘貼http://www.webxml.com.cn中手機查詢服務中的隨便一個地址,命名空間自己設置,如下圖:

   3).點擊上圖中的“確定”按鈕,此時會把這個相關的服務加入到此項目中,如圖:

   4).在winForm中的button源碼如下:

   

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceReference1.MobileCodeWSSoapClient mms = new ServiceReference1.MobileCodeWSSoapClient("MobileCodeWSSoap12"); *
            string s= mms.getMobileCodeInfo(this.textBox1.Text.Trim(),"");
            MessageBox.Show(s);

        }

    *號處的參數是說明用soap的哪種協議的,我們在添加webservice服務后會自動增加一個app.config文件,打開此文件會在文件下面看到如下的代碼:

<client>
            <endpoint address="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"
                binding="basicHttpBinding" bindingConfiguration="MobileCodeWSSoap"
                contract="ServiceReference1.MobileCodeWSSoap" name="MobileCodeWSSoap" />
            <endpoint address="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"
                binding="customBinding" bindingConfiguration="MobileCodeWSSoap12"
                contract="ServiceReference1.MobileCodeWSSoap" name="MobileCodeWSSoap12" />
        </client>

此參數輸入name的值就可以了。

   5).運行效果圖:

   

 

終結:

    在調用時先引用WebService服務,再創建它的實例,然后再調用它的函數即可。

     

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM