XmlHttpRequestProcessor.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:3k
- using System;
- using System.Web;
- using System.Reflection;
- using System.IO;
- namespace AjaxPro
- {
- internal class XmlHttpRequestProcessor : IAjaxProcessor
- {
- private int hashCode;
- internal XmlHttpRequestProcessor(HttpContext context, Type type) : base(context, type)
- {
- }
- #region IAjaxProcessor Members
- public override object[] RetreiveParameters()
- {
- ParameterInfo[] pi = method.GetParameters();
- if(pi.Length == 0)
- return null;
- object[] args = new object[pi.Length];
- // initialize default values
- for(int i=0; i<pi.Length; i++)
- args[i] = pi[i].DefaultValue;
- byte[] b = new byte[context.Request.InputStream.Length];
- if(context.Request.InputStream.Read(b, 0, b.Length) == 0)
- return null;
- StreamReader sr = new StreamReader(new MemoryStream(b), System.Text.UTF8Encoding.UTF8);
- string v = null;
- try
- {
- v = sr.ReadToEnd();
- }
- finally
- {
- sr.Close();
- }
- if(v == null) // something went wrong, we cannot fill the arguments
- return args;
- hashCode = v.GetHashCode();
- // check if we have to decrypt the JSON string.
- if(Utility.Settings != null && Utility.Settings.Encryption != null)
- v = Utility.Settings.Encryption.CryptProvider.Decrypt(v);
- JavaScriptObject jso = (JavaScriptObject)JavaScriptDeserializer.Deserialize(v, typeof(JavaScriptObject));
- for(int i=0; i<pi.Length; i++)
- {
- if(jso.Contains(pi[i].Name))
- args[i] = JavaScriptDeserializer.Deserialize((IJavaScriptObject)jso[pi[i].Name], pi[i].ParameterType);
- }
- return args;
- }
- public override string SerializeObject(object o)
- {
- // On the client we want to have a real object.
- // For more details visit: http://www.crockford.com/JSON/index.html
- //
- // JSON is built on two structures:
- // - A collection of name/value pairs. In various languages,
- // this is realized as an object, record, struct, dictionary,
- // hash table, keyed list, or associative array.
- // - An ordered list of values. In most languages, this is realized
- // as an array.
- string res = JavaScriptSerializer.Serialize(o);
- context.Response.Write(res);
- return res;
- }
- public override MethodInfo Method
- {
- get
- {
- if(context.Request.Headers["Ajax-method"] != null)
- method = type.GetMethod(context.Request.Headers["Ajax-method"]);
- return method;
- }
- }
- public override bool IsEncryptionAble
- {
- get
- {
- return true;
- }
- }
- public override int GetHashCode()
- {
- return hashCode;
- }
- #endregion
- }
- }