1
flash9/flash cs3(as3)通过soap访问Web Services
2
来源:Roading's blog 作者:roading 2007-07-17 【大 中小】
3
4
下面是as3访问Web Services的原理和过程,包括实例和源文件,已经经过了测试(http://www.roading.net/WebService/as3_soap.swf)
5
6
前段时间写了 使用flash9(as3)连接webservice,结果发现这种以http post方法访问WebServices只能在测试环境下使用.然后就写了flash9/as3访问WebService的暂时替代方法,当然这是无奈之举,找不到合适的方法前先使用中转的方法来代替.
7
8
但是还是需要找到真正的解决方法,昨天在翻看flash8的mx\services包的时候,在包里面的SOAPCall和PendingCall类里面有整个的访问方法.
9
10
在SOAPCall类里面有request和response两个对象,分别是提交数据和返回数据.
11
12
下面是节选SOAPCall类的asyncInvoke方法的一部分,实现request的构造和数据发送(这里是流程,具体实现细节在PendingCall类里面):
13
14
15
//callback是PendingCall的实例.
16
callback.encode();
17
18
callback.callbackMethod = callbackMethod; // Callback method
19
20
// Populate parameters
21
callback.setupParams(args);
22
23
// prepare response object
24
var response = new XML();
25
response.ignoreWhite = true;
26
response.callback = callback;
27
response._startTimeMark = startTime;
28
29
30
callback.response = response;
31
32
// create the async response mechanism
33
response.onData = function(src)
34
{
35
}
36
// fire message
37
callback.request.sendAndLoad(this.endpointURI, response, "POST");
38
//-------------------------------------------------------------------------------------------
39
40
41
看到上面的代码,就会豁然开朗,就是使用soap协议,来提交和获取数据.那么,我们就可以很简单的构成一个SOAP 请求.我们看一下soap请求的格式(http://roading.net/WebService/test.asmx?op=say):
42
下面是一个 SOAP 请求和响应示例。所显示的占位符需要由实际值替换。
43
44
45
POST /WebService/test.asmx HTTP/1.1
46
Host: roading.net
47
Content-Type: text/xml; charset=utf-8
48
Content-Length: length
49
SOAPAction: "http://www.roading.net/say"
50
51
<?xml version="1.0" encoding="utf-8"?>
52
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
53
<soap:Body>
54
<say xmlns="http://www.roading.net/">
55
<str>string</str>
56
</say>
57
</soap:Body>
58
</soap:Envelope>
59
60
61
一个soap请求包括头部和数据.
62
soap请求头部包括:
63
64
65
POST /WebService/test.asmx HTTP/1.1
66
Host: roading.net
67
Content-Type: text/xml; charset=utf-8
68
Content-Length: length
69
SOAPAction: http://www.roading.net/say
70
71
72
URLRequestHeader不支持post,host和Content-Length(ArgumentError: Error #2096: HTTP 请求标头 host 不能通过 ActionScript 设置。),同时也不必要,必须设置的是Content-Type和SOAPAction.
73
74
75
//
76
r.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
77
r.requestHeaders.push(new URLRequestHeader("SOAPAction", "http://www.roading.net/say"));
78
//
79
80
81
soap请求数据为:
82
83
84
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
85
<say xmlns="http://www.roading.net/"> //调用方法.. 命名空间
86
<str>hello</str> //参数
87
</say>
88
</soap:Envelope>
89
90
91
整个的soap请求如上面所示
就可以使用URLLoader和URLRequest类来发送和接收数据了.下面是一个完整的调用WebServices的测试代码(不包括解析接收的数据):
92
93
94
//WebService网址(为测试写的例子) http://www.roading.net/WebService/test.asmx
95
import flash.net.*;
96
var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
97
98
var r:URLRequest = new URLRequest("http://www.roading.net/WebService/Test.asmx?op=say");
99
r.method = URLRequestMethod.POST;
100
r.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
101
r.requestHeaders.push(new URLRequestHeader("SOAPAction", "http://www.roading.net/say"));
102
103
104
var rXML:XML =
105
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
106
<soap:Body/>
107
</soap:Envelope>
108
;
109
110
rXML.soap::Body.appendChild(
111
<say xmlns="http://www.roading.net/"> //
112
<str>hello</str> //
113
</say>
114
);
115
116
r.data = rXML;
117
118
var l:URLLoader = new URLLoader();
119
l.dataFormat = URLLoaderDataFormat.TEXT;
120
l.load(r);
121
122
l.addEventListener("ioError" ,err);
123
l.addEventListener(Event.COMPLETE,xmlLoaded);
124
function xmlLoaded(d)
125
{
126
trace(l.data);
127
t.text = l.data;
128
}
129
130
function err(e)
131
{
132
trace(e);
133
}
134

2

3

4


5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91


92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134
