Code
1 这是一个简单的WebService
2
3
4 using System;
5 using System.Collections;
6 using System.Linq;
7 using System.Web;
8 using System.Web.Services;
9 using System.Web.Services.Protocols;
10 using System.Xml.Linq;
11
12 /// <summary>
13 ///WebService 的摘要说明
14 /// </summary>
15 [WebService(Namespace = "http://tempuri.org/")]
16 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
17 //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
18 // [System.Web.Script.Services.ScriptService]
19 public class WebService : System.Web.Services.WebService {
20
21 public WebService () {
22
23 //如果使用设计的组件,请取消注释以下行
24 //InitializeComponent();
25 }
26
27 [WebMethod]
28 public string HelloWorld() {
29 return "Hello World"+DateTime.Now.ToString();
30 }
31
32 }
33
34 客户端调用WebService的代码
35 using System;
36 using System.Collections;
37 using System.Configuration;
38 using System.Data;
39 using System.Linq;
40 using System.Web;
41 using System.Web.Security;
42 using System.Web.UI;
43 using System.Web.UI.HtmlControls;
44 using System.Web.UI.WebControls;
45 using System.Web.UI.WebControls.WebParts;
46 using System.Xml.Linq;
47 using System;
48 using System.Data;
49 using System.Web;
50 using System.Configuration;
51
52 public partial class Service : System.Web.UI.Page
53 {
54 protected void Page_Load(object sender, EventArgs e)
55 {
56 if (!IsPostBack)
57 {
58 Label1.Text = "first loaded at" + DateTime.Now.ToLongTimeString();
59 }
60 else
61 {
62 Label1.Text = "Postback at" + DateTime.Now.ToLongTimeString();
63 }
64 }
65 protected void Button2_Click(object sender, EventArgs e)
66 {
67 localhost.WebService proxy = new localhost.WebService();
68 Label1.Text = "Web Service Returns:" + proxy.HelloWorld();
69 }
70 }
71
1 这是一个简单的WebService
2
3
4 using System;
5 using System.Collections;
6 using System.Linq;
7 using System.Web;
8 using System.Web.Services;
9 using System.Web.Services.Protocols;
10 using System.Xml.Linq;
11
12 /// <summary>
13 ///WebService 的摘要说明
14 /// </summary>
15 [WebService(Namespace = "http://tempuri.org/")]
16 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
17 //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
18 // [System.Web.Script.Services.ScriptService]
19 public class WebService : System.Web.Services.WebService {
20
21 public WebService () {
22
23 //如果使用设计的组件,请取消注释以下行
24 //InitializeComponent();
25 }
26
27 [WebMethod]
28 public string HelloWorld() {
29 return "Hello World"+DateTime.Now.ToString();
30 }
31
32 }
33
34 客户端调用WebService的代码
35 using System;
36 using System.Collections;
37 using System.Configuration;
38 using System.Data;
39 using System.Linq;
40 using System.Web;
41 using System.Web.Security;
42 using System.Web.UI;
43 using System.Web.UI.HtmlControls;
44 using System.Web.UI.WebControls;
45 using System.Web.UI.WebControls.WebParts;
46 using System.Xml.Linq;
47 using System;
48 using System.Data;
49 using System.Web;
50 using System.Configuration;
51
52 public partial class Service : System.Web.UI.Page
53 {
54 protected void Page_Load(object sender, EventArgs e)
55 {
56 if (!IsPostBack)
57 {
58 Label1.Text = "first loaded at" + DateTime.Now.ToLongTimeString();
59 }
60 else
61 {
62 Label1.Text = "Postback at" + DateTime.Now.ToLongTimeString();
63 }
64 }
65 protected void Button2_Click(object sender, EventArgs e)
66 {
67 localhost.WebService proxy = new localhost.WebService();
68 Label1.Text = "Web Service Returns:" + proxy.HelloWorld();
69 }
70 }
71