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服务,再创建它的实例,然后再调用它的函数即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统