以下代码为转载

1. class AMF_Post_Data
2. {
3. public List<byte> message;
4.
5. /// <summary>
6. /// 初始化Message
7. /// </summary>
8. /// <param name="at"></param>
9. /// <param name="headers"></param>
10. /// <param name="bodies"></param>
11. public AMF_Post_Data(AMFType at, int headers, int bodies)
12. {
13. //AMF版本
14. if (at == AMFType.AMF0)
15. {
16. message = new List<byte>(new byte[] { 0x00, 0x00 });
17. }
18. else if (at == AMFType.AMF3)
19. {
20. message = new List<byte>(new byte[] { 0x00, 0x03 });
21. }
22.
23. //header数量
24. message.Add(BitConverter.GetBytes(headers)[1]);
25. message.Add(BitConverter.GetBytes(headers)[0]);
26. //body数量
27. message.Add(BitConverter.GetBytes(bodies)[1]);
28. message.Add(BitConverter.GetBytes(bodies)[0]);
29. }
30.
31. /// <summary>
32. /// 添加Target
33. /// </summary>
34. /// <param name="target"></param>
35. /// <param name="Response"></param>
36. public void AddTargetAndResponse(string target, string Response)
37. {
38. //添加Target长度
39. message.Add(BitConverter.GetBytes(target.Length)[1]);
40. message.Add(BitConverter.GetBytes(target.Length)[0]);
41. //添加Target内容
42. message.AddRange(Encoding.Default.GetBytes(target));
43.
44. //添加Response长度
45. message.Add(BitConverter.GetBytes(Response.Length)[1]);
46. message.Add(BitConverter.GetBytes(Response.Length)[0]);
47. //添加Response内容
48. message.AddRange(Encoding.Default.GetBytes(Response));
49. }
50.
51. /// <summary>
52. /// 添加Body
53. /// </summary>
54. /// <param name="length"></param>
55. /// <param name="Content"></param>
56. public void AddBody(AMF_Post_Data_Body apdb)
57. {
58. message.AddRange(apdb.getLength());
59. message.AddRange(apdb.Content.ToArray());
60. }
61. }
62.
63. class AMF_Post_Data_Body
64. {
65. private byte[] length = new byte[4];
66. public List<byte> Content = new List<byte>();
67.
68. /// <summary>
69. /// 初始化Body
70. /// </summary>
71. /// <param name="dt"></param>
72. /// <param name="ArrayLength"></param>
73. public AMF_Post_Data_Body(DataType dt, int ArrayLength)
74. {
75. //添加类型标识
76. Content.Add((byte)dt);
77.
78. //数组的话添加长度
79. if (dt == DataType.Array)
80. {
81. Content.Add(BitConverter.GetBytes(ArrayLength)[3]);
82. Content.Add(BitConverter.GetBytes(ArrayLength)[2]);
83. Content.Add(BitConverter.GetBytes(ArrayLength)[1]);
84. Content.Add(BitConverter.GetBytes(ArrayLength)[0]);
85. }
86. }
87.
88. public void AddData(DataType dt, string value)
89. {
90. //添加类型标识
91. Content.Add((byte)dt);
92.
93. switch (dt)
94. {
95. case DataType.Number:
96. AddData_Number(double.Parse(value));
97. break;
98. case DataType.String:
99. AddData_String(value);
100. break;
101. case DataType.Boolean:
102. AddData_Boolean(Boolean.Parse(value));
103. break;
104. }
105. }
106.
107. #region 更种类型处理方法
108. /// <summary>
109. /// Boolean
110. /// </summary>
111. /// <param name="p"></param>
112. private void AddData_Boolean(bool p)
113. {
114. if (p)
115. Content.Add(0x01);
116. else
117. Content.Add(0x00);
118. }
119. /// <summary>
120. /// String
121. /// </summary>
122. /// <param name="value"></param>
123. private void AddData_String(string value)
124. {
125. //添加长度
126. Content.Add(BitConverter.GetBytes(value.Length)[1]);
127. Content.Add(BitConverter.GetBytes(value.Length)[0]);
128. //添加内容
129. Content.AddRange(Encoding.Default.GetBytes(value));
130. }
131. /// <summary>
132. /// Number
133. /// </summary>
134. /// <param name="p"></param>
135. private void AddData_Number(double p)
136. {
137. byte[] b = new byte[8];
138. b = BitConverter.GetBytes(p);
139. for (int i = 7; i > -1; i--)
140. {
141. Content.Add(b[i]);
142. }
143. }
144. #endregion
145.
146. public byte[] getLength()
147. {
148. length[0] = BitConverter.GetBytes(Content.Count)[3];
149. length[1] = BitConverter.GetBytes(Content.Count)[2];
150. length[2] = BitConverter.GetBytes(Content.Count)[1];
151. length[3] = BitConverter.GetBytes(Content.Count)[0];
152.
153. return length;
154. }
155. }
156.
157. #region 类型枚举
158. public enum AMFType
159. {
160. AMF0,
161. AMF3
162. }
163.
164. public enum DataType
165. {
166. Number = 0,
167. Boolean = 1,
168. String = 2,
169. UntypedObject = 3,
170. MovieClip = 4,
171. Null = 5,
172. Undefined = 6,
173. ReferencedObject = 7,
174. MixedArray = 8,
175. End = 9,
176. Array = 10,//0x0A
177. Date = 11,//0x0B
178. LongString = 12,//0x0C
179. TypeAsObject = 13,//0x0D
180. Recordset = 14,//0x0E
181. Xml = 15,//0x0F
182. TypedObject = 16,//0x10
183. AMF3data = 17//0x11
184. }
185. #endregion
2. {
3. public List<byte> message;
4.
5. /// <summary>
6. /// 初始化Message
7. /// </summary>
8. /// <param name="at"></param>
9. /// <param name="headers"></param>
10. /// <param name="bodies"></param>
11. public AMF_Post_Data(AMFType at, int headers, int bodies)
12. {
13. //AMF版本
14. if (at == AMFType.AMF0)
15. {
16. message = new List<byte>(new byte[] { 0x00, 0x00 });
17. }
18. else if (at == AMFType.AMF3)
19. {
20. message = new List<byte>(new byte[] { 0x00, 0x03 });
21. }
22.
23. //header数量
24. message.Add(BitConverter.GetBytes(headers)[1]);
25. message.Add(BitConverter.GetBytes(headers)[0]);
26. //body数量
27. message.Add(BitConverter.GetBytes(bodies)[1]);
28. message.Add(BitConverter.GetBytes(bodies)[0]);
29. }
30.
31. /// <summary>
32. /// 添加Target
33. /// </summary>
34. /// <param name="target"></param>
35. /// <param name="Response"></param>
36. public void AddTargetAndResponse(string target, string Response)
37. {
38. //添加Target长度
39. message.Add(BitConverter.GetBytes(target.Length)[1]);
40. message.Add(BitConverter.GetBytes(target.Length)[0]);
41. //添加Target内容
42. message.AddRange(Encoding.Default.GetBytes(target));
43.
44. //添加Response长度
45. message.Add(BitConverter.GetBytes(Response.Length)[1]);
46. message.Add(BitConverter.GetBytes(Response.Length)[0]);
47. //添加Response内容
48. message.AddRange(Encoding.Default.GetBytes(Response));
49. }
50.
51. /// <summary>
52. /// 添加Body
53. /// </summary>
54. /// <param name="length"></param>
55. /// <param name="Content"></param>
56. public void AddBody(AMF_Post_Data_Body apdb)
57. {
58. message.AddRange(apdb.getLength());
59. message.AddRange(apdb.Content.ToArray());
60. }
61. }
62.
63. class AMF_Post_Data_Body
64. {
65. private byte[] length = new byte[4];
66. public List<byte> Content = new List<byte>();
67.
68. /// <summary>
69. /// 初始化Body
70. /// </summary>
71. /// <param name="dt"></param>
72. /// <param name="ArrayLength"></param>
73. public AMF_Post_Data_Body(DataType dt, int ArrayLength)
74. {
75. //添加类型标识
76. Content.Add((byte)dt);
77.
78. //数组的话添加长度
79. if (dt == DataType.Array)
80. {
81. Content.Add(BitConverter.GetBytes(ArrayLength)[3]);
82. Content.Add(BitConverter.GetBytes(ArrayLength)[2]);
83. Content.Add(BitConverter.GetBytes(ArrayLength)[1]);
84. Content.Add(BitConverter.GetBytes(ArrayLength)[0]);
85. }
86. }
87.
88. public void AddData(DataType dt, string value)
89. {
90. //添加类型标识
91. Content.Add((byte)dt);
92.
93. switch (dt)
94. {
95. case DataType.Number:
96. AddData_Number(double.Parse(value));
97. break;
98. case DataType.String:
99. AddData_String(value);
100. break;
101. case DataType.Boolean:
102. AddData_Boolean(Boolean.Parse(value));
103. break;
104. }
105. }
106.
107. #region 更种类型处理方法
108. /// <summary>
109. /// Boolean
110. /// </summary>
111. /// <param name="p"></param>
112. private void AddData_Boolean(bool p)
113. {
114. if (p)
115. Content.Add(0x01);
116. else
117. Content.Add(0x00);
118. }
119. /// <summary>
120. /// String
121. /// </summary>
122. /// <param name="value"></param>
123. private void AddData_String(string value)
124. {
125. //添加长度
126. Content.Add(BitConverter.GetBytes(value.Length)[1]);
127. Content.Add(BitConverter.GetBytes(value.Length)[0]);
128. //添加内容
129. Content.AddRange(Encoding.Default.GetBytes(value));
130. }
131. /// <summary>
132. /// Number
133. /// </summary>
134. /// <param name="p"></param>
135. private void AddData_Number(double p)
136. {
137. byte[] b = new byte[8];
138. b = BitConverter.GetBytes(p);
139. for (int i = 7; i > -1; i--)
140. {
141. Content.Add(b[i]);
142. }
143. }
144. #endregion
145.
146. public byte[] getLength()
147. {
148. length[0] = BitConverter.GetBytes(Content.Count)[3];
149. length[1] = BitConverter.GetBytes(Content.Count)[2];
150. length[2] = BitConverter.GetBytes(Content.Count)[1];
151. length[3] = BitConverter.GetBytes(Content.Count)[0];
152.
153. return length;
154. }
155. }
156.
157. #region 类型枚举
158. public enum AMFType
159. {
160. AMF0,
161. AMF3
162. }
163.
164. public enum DataType
165. {
166. Number = 0,
167. Boolean = 1,
168. String = 2,
169. UntypedObject = 3,
170. MovieClip = 4,
171. Null = 5,
172. Undefined = 6,
173. ReferencedObject = 7,
174. MixedArray = 8,
175. End = 9,
176. Array = 10,//0x0A
177. Date = 11,//0x0B
178. LongString = 12,//0x0C
179. TypeAsObject = 13,//0x0D
180. Recordset = 14,//0x0E
181. Xml = 15,//0x0F
182. TypedObject = 16,//0x10
183. AMF3data = 17//0x11
184. }
185. #endregion