站内搜索: 请输入搜索关键词

当前页面: JAVA 编程资料牛鼻论坛Java XML, Web Service 技术区→从 Windows 窗体调用 XML Web service

从 Windows 窗体调用 XML Web service

发表新主题   回复此主题

第1楼 2006-11-29 14:30 cmg4476 写道:

从 Windows 窗体调用 XML Web service


从 Windows 窗体如何调用 XML Web service?

第2楼 2013-08-31 12:44 Robot :

从 Windows 窗体调用 XML Web service 相关


第3楼 2006-11-29 14:42 chumaogao 写道:

演练:从 Windows 窗体调用 XML Web services XML Web services 是 Visual Studio 的一个新功能,它提供在松耦合环境中使用标准协议(如 HTTP、XML、XSD、SOAP 和 WSDL)交换消息的功能。可以结构化和类型化这些消息或对这些消息进行松散定义。因为 Web 服务基于标准协议,所以 Web 服务应用程序可以与各种不同的实现、平台和设备通讯。有关更多信息,请参阅托管代码中的 XML Web services。
可以使用 Web 服务增强 Windows 窗体功能。连接 Windows 窗体和 Web 服务与调用 Web 服务方法一样简单,这些方法在服务器上进行处理,然后返回方法调用的结果。
有两种类型的 Web 服务方法:同步和异步。当调用同步 Web 服务方法时,调用方等待 Web 服务响应后再继续执行操作。当调用异步 Web 服务方法时,可以在等待 Web 服务响应的同时继续使用调用线程。这使得您能够在客户端应用程序中有效地使用现有的线程集合。有关使用同步和异步 Web 服务方法的更多信息,请参阅使用托管代码访问 XML Web services。
同步 Web 服务方法
调用同步 Web 服务方法包括调用该方法;等待在服务器上进行的计算并返回一个值;然后再继续执行 Windows 窗体中的其他代码。
创建 XML Web services



创建 Web 服务应用程序。有关更多信息,请参阅创建托管代码中的 XML Web services。

在解决方案资源管理器中,用右键单击 .asmx 文件并选择“查看代码”。

创建执行相加的 Web 服务方法。以下 Web 服务方法将两个整数相加,然后返回两者的和:

4. ' Visual Basic
5. <WebMethod()> Public Function WebAdd(ByVal x As Integer, ByVal y As Integer) As Integer
6. Return x + y
7. End Function
8.
9. // C#
10. [WebMethod]
11. public int WebAdd(int x, int y)
12. {
13. return x + y;
}



创建另一个执行相乘的 Web 服务方法。以下 Web 服务方法将两个整数相乘,并返回两者的积:

15. ' Visual Basic
16. <WebMethod()> Public Function WebMultiply(ByVal x As Integer, ByVal y As Integer) As Integer
17. Return x * y
18. End Function
19.
20. // C#
21. [WebMethod]
22. public int WebMultiply(int x, int y)
23. {
24. return x * y;
}



从“生成”菜单中,选择“生成解决方案”。也可以浏览到在此项目中创建的 .asmx 文件,以便了解 Web 服务的更多信息。现在就可以从 Windows 窗体调用 Web 服务了。

同步调用 XML Web services



创建新的 Windows 应用程序。有关更多信息,请参阅创建 Windows 应用程序项目。

添加对上面创建的 Web 服务的引用。详细信息,请参阅添加和移除 Web 引用。

从工具箱中,添加三个 TextBox 控件和两个 Button 控件。文本框用于数字,按钮则用于计算和调用 Web 服务方法。

按以下方式设置控件的属性:


控件
属性
文本

TextBox1
Text
0

TextBox2
Text
0

TextBox3
Text
0

Button1
Text
相加

Button2
Text
相乘




用右键单击该窗体并选择“查看代码”。

将 Web 服务的实例创建为类成员。需要知道创建上述 Web 服务所在的服务器名称。

7. ' Visual Basic
8. ' Replace localhost below with the name of the server where
9. ' you created the Web service.
10. Dim MathServiceClass As New localhost.Service1()
11.
12. // C#
localhost.Service1 MathServiceClass = new localhost.Service1();



为 Button1 的 Click 事件创建事件处理程序。详细信息,请参阅在“Windows 窗体设计器”上创建事件处理程序。

14. ' Visual Basic
15. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
16. ' Create instances of the operands and result.
17. Dim x, y, z As Integer
18. ' Parse the contents of the text boxes into integers.
19. x = Integer.Parse(TextBox1.Text)
20. y = Integer.Parse(TextBox2.Text)
21. ' Call the WebAdd Web service method from the instance of the Web service.
22. z = MathServiceClass.WebAdd(x, y)
23. TextBox3.Text = z.ToString
24. End Sub
25.
26. // C#
27. private void button1_Click(object sender, System.EventArgs e)
28. {
29. // Create instances of the operands and result.
30. int x, y, z;
31. // Parse the contents of the text boxes into integers.
32. x = int.Parse(textBox1.Text);
33. y = int.Parse(textBox2.Text);
34. // Call the WebAdd Web service method from the instance of the Web service.
35. z = MathServiceClass.WebAdd(x, y);
36. textBox3.Text = z.ToString();
}



以相同方式为 Button2 的 Click 事件创建事件处理程序,并添加以下代码。

38. ' Visual Basic
39. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
40. ' Create instances of the operands and result.
41. Dim x, y, z As Integer
42. ' Parse the contents of the text boxes into integers.
43. x = Integer.Parse(TextBox1.Text)
44. y = Integer.Parse(TextBox2.Text)
45. ' Call the WebMultiply Web service method from the instance of the Web service.
46. z = MathServiceClass.WebMultiply(x, y)
47. TextBox3.Text = z.ToString
48. End Sub
49.
50. // C#
51. private void button2_Click(object sender, System.EventArgs e)
52. {
53. // Create instances of the operands and result.
54. int x, y, z;
55. // Parse the contents of the text boxes into integers.
56. x = int.Parse(textBox1.Text);
57. y = int.Parse(textBox2.Text);
58. // Call the WebAdd Web service method from the instance of the Web service.
59. z = MathServiceClass.WebMultiply(x, y);
60. textBox3.Text = z.ToString();
}



按 F5 键运行应用程序。在前两个文本框中输入值。当按“添加”按钮时,第三个文本框将显示两个值的和。当按“乘”按钮时,第三个文本框将显示两个值的积。


第4楼 2009-05-06 04:03 w2009 写道:

*** 该用户已经被管理员删除 ***

发表新主题   回复此主题