WebService是运行于服务端(通常放在信息服务器上的)让客户端来调用的。html
如下开发两个简单的实例web
1.本身开发服务端本身调用(vs2010)浏览器
1).菜单:“新建-项目”,在打开的窗体中选择,以下图:服务器
2).在“项目解决方案”中右击此项目并“添加-新建项”,而后选择"web服务",以下图app
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).开发客户端。this
<%@ 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源码(按钮事件):spa
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",能够看到以下图:code
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服务,再建立它的实例,而后再调用它的函数便可。