简述WebService的使用(一)

环境:

  vs版本:vs2013

  windows版本:win7

  IIS版本:IIS7.0

  (如果觉得对您有用,请点击右下角【推荐】一下,让更多人看到,谢谢)

配置环境:

  主要针对于IIS

  ·首先,有很多人的机器上都没有打开IIS服务

    控制面板->程序和功能->打开或关闭windows功能(左侧,较慢,稍等)->Internet信息服务(默认打开的功能不能完全满足之后的需要,可以全部打开或者网上查询一下需要打开哪些)

  ·接着,在管理工具中打开Internet 信息服务(IIS)管理器

  ·最后,在网页上输入http://127.0.0.1后能看到IIS的主页,就ok了。(这里隐藏了一个问题,就是先安装了framework后安装IIS会有一个问题,稍后解决)

  

  防火墙配置:(如不配置,在其他机器上访问不到发布在你机器上的服务接口或者其他网站)

  网上有说直接关了防火墙,就好比人家惹到你,你非得整死他一样。。。好惨

  打开防火墙,点击左侧菜单里面的“高级设置”,会看到有“入站规则”和“出站规则”,添加一个入站规则端口就好了,这样,你在下面的流程中配置的那个端口,在其他位置访问你机器上的这个端口的时候,就不会被拦住了...何必置人于死地呢!

实现过程之编写WebService

  我使用的是vs2013,过程如下:

  1、创建空解决方案

  2、创建空Web应用程序工程(这里面没有web服务工程...)

  3、创建Web服务(asmx)

  这是IDE会给你初始化一个开发框架,你只需要在里面加上你需要公开的方法就可以了,[WebService]特性的使用就是用来修饰将要公布出来的服务接口。(具体原理这里不讲)

  代码如下:

简述WebService的使用(一)
 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Web;

 5 using System.Web.Services;

 6 

 7 namespace WebService

 8 {

 9     /// <summary>

10     /// WebService1 的摘要说明

11     /// </summary>

12     [WebService(Namespace = "http://MrHouJL/WebServices")]

13     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

14     [System.ComponentModel.ToolboxItem(false)]

15     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 

16     [System.Web.Script.Services.ScriptService]

17     public class WebService1 : System.Web.Services.WebService

18     {

19 

20         [WebMethod]

21         public string HelloWorld(string str)

22         {

23             return "Hello World" + str;

24         }

25         [WebMethod]

26         public string HelloWorld1()

27         {

28             return "Hello World 1";

29         }

30         [WebMethod]

31         public string HelloWorld2()

32         {

33             return "Hello World 2";

34         }

35         [WebMethod]

36         public string HelloWorld3()

37         {

38             return "Hello World 3";

39         }

40         [WebMethod(Description = "求和的方法")]

41         public double addition(double i, double j)

42         {

43             return i + j;

44         }

45         [WebMethod(Description = "求差的方法")]

46         public double subtract(double i, double j)

47         {

48             return i - j;

49         }

50         [WebMethod(Description = "求积的方法")]

51         public double multiplication(double i, double j)

52         {

53             return i * j;

54         }

55         [WebMethod(Description = "求商的方法")]

56         public double division(double i, double j)

57         {

58             if (j != 0)

59                 return i / j;

60             else

61                 return 0;

62         }

63     }

64 }
View Code

  功能编写完毕,接下来就是发布在刚刚准备好的IIS环境上面了。

  1、右键点击工程,发布,选择一个文件夹物理路径。

  2、打开IIS管理器

  3、右击“网站”,添加网站,配置“网站名称”,“物理路径”,“IP”,“端口”OK

  4、注意:这里面的身份验证要允许匿名,目录浏览要启用(双击点击右侧启用,为了之后可以浏览WebService目录)

  5、运行网站,你在浏览器中输入之前输入的IP+端口号就能访问到目录了(如有问题留言或者度娘)。

实现过程之访问WebService

  在这里主要是介绍使用后台访问:

  首先,在工程里面右键点击引用,添加服务引用,输入IP+端口,点击“转到”,应该就能看到之前的所写的服务接口了。起个名就添加进去了。

  添加webform界面,前台代码如下

简述WebService的使用(一)
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebService.WebForm1" %>

 2 

 3 <!DOCTYPE html>

 4 

 5 <html xmlns="http://www.w3.org/1999/xhtml">

 6 <head runat="server">

 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

 8     <title></title>

 9 </head>

10 <body>

11     <form id="form1" runat="server">

12     <div>

13     

14     </div>

15         <asp:TextBox ID="TextBox1" runat="server" Height="15px" Width="50px"></asp:TextBox>

16 &nbsp;<asp:DropDownList ID="DropDownList1" runat="server">

17             <asp:ListItem>+</asp:ListItem>

18             <asp:ListItem>-</asp:ListItem>

19             <asp:ListItem>*</asp:ListItem>

20             <asp:ListItem>/</asp:ListItem>

21         </asp:DropDownList>

22 &nbsp;<asp:TextBox ID="TextBox2" runat="server" Height="15px" Width="50px"></asp:TextBox>

23 &nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text=" = " />

24 &nbsp;<asp:TextBox ID="TextBox3" runat="server" Height="15px" Width="50px"></asp:TextBox>

25     </form>

26 </body>

27 </html>
View Code

  后台代码如下:

简述WebService的使用(一)
 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Web;

 5 using System.Web.UI;

 6 using System.Web.UI.WebControls;

 7 

 8 namespace WebService

 9 {

10     public partial class WebForm1 : System.Web.UI.Page

11     {

12         protected void Page_Load(object sender, EventArgs e)

13         {

14 

15         }

16 

17         protected void Button1_Click(object sender, EventArgs e)

18         {

19             string oper = DropDownList1.Text;

20             double a = Convert.ToDouble(TextBox1.Text);

21             double b = Convert.ToDouble(TextBox2.Text);

22             ServiceReference1.WebService1SoapClient ws = new ServiceReference1.WebService1SoapClient();

23             switch (oper)

24             {

25                 case "+": TextBox3.Text = ws.addition(a, b).ToString(); break;

26                 case "-": TextBox3.Text = ws.subtract(a, b).ToString(); break;

27                 case "*": TextBox3.Text = ws.multiplication(a, b).ToString(); break;

28                 case "/": TextBox3.Text = ws.division(a, b).ToString(); break;

29                 default:

30                     break;

31             }

32             Response.Write(ws.HelloWorld(TextBox3.Text));

33         }

34     }

35 }
View Code

  建议各位小主,还是自己写写较好。

遇到的问题

  HTTP 错误 500.21 - Internal Server Error处理程序“NickLeeCallbackHandler”在其模块列表中有一个错误模块“ManagedPipelineHandler”

  

    原因:在安装Framework v4.0之后,再启用IIS,导致Framework没有完全安装

  解决:开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

  如果还不行,可检查IIS的应用程序池,是否使用集成模式,如果不是则改成集成模式

 

  至此,整个webservice入门就告一段落了,大家仅作参考,如有问题,快来指正...

你可能感兴趣的:(webservice)