實戰基礎技能(18)--------用ASP.NET調用Web Service

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   for   

開啟VS2010,開啟“檔案-建立-網站”,選擇“ASP.NET網站”

選好儲存位置,語言後點擊確定,進入預設頁面。然後先添加Web引用,把WebService引到當前的工程裡面。方法是:在資源管理員中點擊右鍵,選擇添加Web 引用,調出對話方塊:

 

在URL中填入,前面寫好的WebService運行後瀏覽器上面顯示的地址,點擊“前往”按鈕,如,就會顯示出所引用的WebService中可以調用的方法,然後點擊“添加引用”,就將webservice引用到了當前的工程裡面 ,如,解決方案中會出現引進來的WebService檔案

我們在這就練習調用webservice的四個方法,做一個簡單的調用的例子,先在網站的前台添加幾個控制項,代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %><!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 id="Head1" runat="server">    <title>Webservice調用執行個體</title></head><body>    <form id="form2" runat="server">        <div>            <asp:TextBox ID="Num1" runat="server"></asp:TextBox>            <select id="selectOper" runat = "server">                <option>+</option>                <option>-</option>                <option>*</option>                <option>/</option>            </select>            <asp:TextBox ID="Num2" runat="server"></asp:TextBox>            <span id = E runat = "server"></span>            <asp:TextBox ID="Result" runat="server"></asp:TextBox>        </div></form></body></html>
然後在後台寫調用的代碼,調用之前和使用其它的對象一樣,要先執行個體化,執行個體化的方法是localhost.Service a =new localhost.Service();然後就可以通過a來訪問WebService裡面提供的方法了。在這個例子裡面,動態建立了一個button控制項來觸發WebService的調用,後台代碼如下:
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApplication2{    public partial class _Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            //在頁面載入的時候動態建立一個按鈕,在它的事件裡調用Webservice            Button btn = new Button();            btn.Width = 20;            btn.Text = " = ";            btn.Click += new EventHandler(btn_Click);            E.Controls.Add(btn);        }        /// <summary>        /// 定義動態建立Button的Click事件,在這個事件中調用Webservice        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        void btn_Click(object sender, EventArgs e)        {            if (Num1.Text != "" && Num2.Text != "")            {                //執行個體化引用的webservice對象                localhost.Service1 WebserviceInstance = new localhost.Service1();                int Oper = selectOper.SelectedIndex;                switch (Oper)                {                    //通過執行個體化的webservice對象來調用Webservice暴露的方法                    case 0:                        Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();                        break;                    case 1:                        Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();                        break;                    case 2:                        Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();                        break;                    case 3:                        Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();                        break;                }            }        }    }}
運行後可以看到效果,如所示,在前面兩個Textbox裡面輸入兩個運算元,在中間的下拉式清單中選擇操作符,然後點擊“=”號,將計算的結果輸出到第三個Textbox裡面。

 

其中的報錯解決

整個計算並不是在本地進行的,是在Web服務端進行計算的然後將結果通過XML返還給了調用方的,所以,在運行該程式的時候,WebService程式還必須啟動,否則會報無法串連遠程伺服器的異常

 

實戰基礎技能(18)--------用ASP.NET調用Web Service

聯繫我們

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