Example #1
0
 //Possibility to query (documentation): http://www.random.org/clients/http/
 private void button1_Click(object sender, EventArgs e)
 {
     var panel = this.Notify("Please wait...", -1, 0.3, Color.Green, Color.Black, Color.White);
     var options = new AjaxRequestOptions
     {
         //You can also delete the Data object and add this to the URL:
         //?min=1&max=52&col=1&format=plain&rnd=new
         //It will behave identical, butshow that both options (Data and URL) are possible.
         Data = new {
             min = 1,
             max = 52,
             col = 1,
             format = "plain",
             rnd = "new"
         },
         Url = textBox2.Text,
         Type = (AjaxRequestType)Enum.Parse(typeof(AjaxRequestType), comboBox1.SelectedItem.ToString()),
         Complete = (s, ev) =>
         {
             Controls.Remove(panel);
             panel.Dispose();
         },
         Success = (s, ev) =>
         {
             textBox1.Text = ev.ResponseText.Replace("\n", "\r\n");
             textBox3.Text = MakeHeaders(s);
         }
     };
     options.AddStatusCodeHandler(System.Net.HttpStatusCode.OK, (s, ev) =>
     {
         MessageBox.Show("I received something with StatusCode 200!");
     });
     AjaxRequest.Create(options);
 }
Example #2
0
        /// <summary>
        /// Performs an AJAX reuqest with custom options.
        /// </summary>
        /// <param name="options">The custom options of the current request as an anonymous object.</param>
        /// <returns>The AjaxRequest object.</returns>
        public static AjaxRequest Ajax(object options)
        {
            var opt = new AjaxRequestOptions();
            var oft = typeof(AjaxRequestOptions);
            var rft = options.GetType().GetProperties();

            foreach (var rf in rft)
            {
                if (oft.GetProperty(rf.Name) != null)
                    oft.GetProperty(rf.Name).SetValue(opt, rf.GetValue(options, null), null);
            }

            return Ajax(opt);
        }
Example #3
0
        /// <summary>
        /// Performs an AJAX reuqest with custom options.
        /// </summary>
        /// <param name="options">The custom options of the current request as an anonymous object.</param>
        /// <returns>The AjaxRequest object.</returns>
        public static AjaxRequest Ajax(object options)
        {
            var opt = new AjaxRequestOptions();
            var oft = typeof(AjaxRequestOptions);
            var rft = options.GetType().GetProperties();

            foreach (var rf in rft)
            {
                if (oft.GetProperty(rf.Name) != null)
                {
                    oft.GetProperty(rf.Name).SetValue(opt, rf.GetValue(options, null), null);
                }
            }

            return(Ajax(opt));
        }
Example #4
0
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="options"></param>
        public AjaxRequest(AjaxRequestOptions options)
        {
            Options = options;
            header = new Dictionary<string, string>();
            body = string.Empty;
            var url = options.DirectUrl;

            if (options.Type == AjaxRequestType.GET)
                url = options.DirectUrl + "?" + options.EncodedNameValuePairs;

            request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method = Options.Type.ToString();
            request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            request.Timeout = Options.Timeout * 1000;

            if(!string.IsNullOrEmpty(Options.MimeType))
                request.ContentType = Options.MimeType;

            request.AllowAutoRedirect = true;
            request.Date = DateTime.Now;

            if(!string.IsNullOrEmpty(Options.Accepts))
                request.Accept = Options.Accepts;

            if (!string.IsNullOrEmpty(Options.UserName))
                SetAuthorizationHeader(Options.UserName, Options.Password ?? string.Empty);

            // See: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
            if(!Options.DiscardRequestedWith)
                request.Headers.Add("X-Requested-With", "XMLHttpRequest");

            foreach (string key in Options.Headers.Keys)
                request.Headers[key] = Options.Headers[key].ToString();

            if (Options.Complete != null)
                OnComplete += Options.Complete;

            if (Options.Success != null)
                OnSuccess += Options.Success;

            if (Options.Error != null)
                OnError += Options.Error;

            task = new Task(PerformRequest);
            ReadyState = WebX.ReadyState.Uninitialized;
        }
Example #5
0
 /// <summary>
 /// Performs an AJAX reuqest with custom options.
 /// </summary>
 /// <param name="url">The url where the request is sent to.</param>
 /// <param name="options">The custom options of the current request.</param>
 /// <returns>The AjaxRequest object.</returns>
 public static AjaxRequest Ajax(this Uri url, AjaxRequestOptions options)
 {
     options.Url = url.ToString();
     return Ajax(options);
 }
Example #6
0
 /// <summary>
 /// The Constructor.
 /// </summary>
 /// <param name="options">The given AjaxRequest options.</param>
 /// <param name="data">The received data as a string.</param>
 public DataFilterEventArgs(AjaxRequestOptions options, string data) : base(options)
 {
     OriginalData = data;
     ModifiedData = data;
 }
Example #7
0
 /// <summary>
 /// Performs an AJAX reuqest with custom options.
 /// </summary>
 /// <param name="options">The custom options of the current request.</param>
 /// <returns>The AjaxRequest object.</returns>
 public static AjaxRequest Ajax(AjaxRequestOptions options)
 {
     return(new AjaxRequest(options));
 }
Example #8
0
 /// <summary>
 /// Creates (i.e. spawns an instance and starts the request) a new AjaxRequest.
 /// </summary>
 /// <param name="options">An AjaxRequestOptions instance with all options set.</param>
 /// <returns>The AjaxRequest instance.</returns>
 public static AjaxRequest Create(AjaxRequestOptions options)
 {
     return new AjaxRequest(options).Invoke();
 }
Example #9
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="options">We expect some options here.</param>
 public AjaxEventArgs(AjaxRequestOptions options)
 {
     Options = options;
 }
Example #10
0
 public AjaxResponseEventArgs(AjaxRequestOptions options, string text, object response) : base(options)
 {
     ResponseText = text;
     Response     = response;
 }
Example #11
0
        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="options"></param>
        public AjaxRequest(AjaxRequestOptions options)
        {
            Options = options;
            header  = new Dictionary <string, string>();
            body    = string.Empty;
            var url = options.DirectUrl;

            if (options.Type == AjaxRequestType.GET)
            {
                url = options.DirectUrl + "?" + options.EncodedNameValuePairs;
            }

            request             = HttpWebRequest.Create(url) as HttpWebRequest;
            request.Method      = Options.Type.ToString();
            request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
            request.Timeout     = Options.Timeout * 1000;

            if (!string.IsNullOrEmpty(Options.MimeType))
            {
                request.ContentType = Options.MimeType;
            }

            request.AllowAutoRedirect = true;
            request.Date = DateTime.Now;

            if (!string.IsNullOrEmpty(Options.Accepts))
            {
                request.Accept = Options.Accepts;
            }

            if (!string.IsNullOrEmpty(Options.UserName))
            {
                SetAuthorizationHeader(Options.UserName, Options.Password ?? string.Empty);
            }

            // See: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
            if (!Options.DiscardRequestedWith)
            {
                request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            }

            foreach (string key in Options.Headers.Keys)
            {
                request.Headers[key] = Options.Headers[key].ToString();
            }

            if (Options.Complete != null)
            {
                OnComplete += Options.Complete;
            }

            if (Options.Success != null)
            {
                OnSuccess += Options.Success;
            }

            if (Options.Error != null)
            {
                OnError += Options.Error;
            }

            task       = new Task(PerformRequest);
            ReadyState = WebX.ReadyState.Uninitialized;
        }
 /// <summary>
 /// The Constructor.
 /// </summary>
 /// <param name="options">The given AjaxRequest options.</param>
 /// <param name="data">The received data as a string.</param>
 public DataFilterEventArgs(AjaxRequestOptions options, string data)
     : base(options)
 {
     OriginalData = data;
     ModifiedData = data;
 }
Example #13
0
        /// <summary>
        /// Creates (i.e. spawns an instance and starts the request) a new AjaxRequest.
        /// </summary>
        /// <param name="options">An anonymous object with all necessary options set.</param>
        /// <returns>The AjaxRequest instance.</returns>
        public static AjaxRequest Create(object options)
        {
            var opt = new AjaxRequestOptions();
            var oprops = opt.GetType().GetProperties();
            var props = options.GetType().GetProperties();

            foreach (var prop in props)
            {
                foreach (var oprop in oprops)
                {
                    if (prop.Name.Equals(oprop))
                    {
                        oprop.SetValue(opt, prop.GetValue(options, null), null);
                        break;
                    }
                }
            }

            return AjaxRequest.Create(opt);
        }
Example #14
0
 /// <summary>
 /// Performs an AJAX reuqest with custom options.
 /// </summary>
 /// <param name="options">The custom options of the current request.</param>
 /// <returns>The AjaxRequest object.</returns>
 public static AjaxRequest Ajax(AjaxRequestOptions options)
 {
     return new AjaxRequest(options);
 }
Example #15
0
 /// <summary>
 /// Performs an AJAX reuqest with custom options.
 /// </summary>
 /// <param name="url">The url where the request is sent to.</param>
 /// <param name="options">The custom options of the current request.</param>
 /// <returns>The AjaxRequest object.</returns>
 public static AjaxRequest Ajax(this Uri url, AjaxRequestOptions options)
 {
     options.Url = url.ToString();
     return(Ajax(options));
 }
Example #16
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="options">We expect some options here.</param>
 public AjaxEventArgs(AjaxRequestOptions options)
 {
     Options = options;
 }
 public AjaxResponseEventArgs(AjaxRequestOptions options, string text, object response)
     : base(options)
 {
     ResponseText = text;
     Response = response;
 }