//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); }
/// <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); }
/// <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)); }
/// <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> /// 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); }
/// <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; }
/// <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)); }
/// <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(); }
/// <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; }
/// <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> /// 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); }
/// <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); }
/// <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)); }