/// <summary> /// Start an async request, and return a unique ID that identifies it. /// </summary> /// /// <param name="url"> /// The URL of the remote server. /// </param> /// <param name="success"> /// A delegate to invoke upon successful completion of the request. /// </param> /// <param name="fail"> /// A delegate to invoke when a request fails. /// </param> /// <param name="options"> /// Options to be used when creating this request. If not provided, the default options will be /// used. /// </param> /// /// <returns> /// A unique identifier that can be used to track this request when it resolves. /// </returns> public static int StartAsyncWebRequest(string url, Action<ICsqWebResponse> success, Action<ICsqWebResponse> fail, ServerConfig options = null) { int id = GetAsyncRequestID(); StartAsyncWebRequest(url, success, fail, id, options); return id; }
/// <summary> /// Configure the "default default" settings. /// </summary> static ServerConfig() { _Default = new ServerConfig { Timeout = TimeSpan.FromSeconds(10), UserAgent = "Mozilla/5.0 (compatible; CsQuery/1.3)" }; }
/// <summary> /// Configure the "default default" settings. /// </summary> static ServerConfig() { _Default = new ServerConfig { Timeout = TimeSpan.FromSeconds(10), UserAgent = null }; }
/// <summary> /// Creates a new DOM from an HTML file. /// </summary> /// /// <param name="url"> /// The URL of the remote server. /// </param> /// <param name="options"> /// The options to use when creating the reqest. /// </param> /// /// <returns> /// A CQ object composed from the HTML response from the server. /// </returns> public static CQ CreateFromUrl(string url, ServerConfig options=null) { CsqWebRequest request = new CsqWebRequest(url); ServerConfig.Apply(options, request); request.Get(); return CQ.CreateDocument(request.Html); }
/// <summary> /// Start an async request, and return a unique ID that identifies it. /// </summary> /// /// <param name="url"> /// The URL of the remote server. /// </param> /// <param name="success"> /// A delegate to invoke upon successful completion of the request. /// </param> /// <param name="fail"> /// A delegate to invoke when a request fails. /// </param> /// <param name="id"> /// The identifier. /// </param> /// <param name="options"> /// Options to be used when creating this request. If not provided, the default options will be /// used. /// </param> public static void StartAsyncWebRequest(string url, Action<ICsqWebResponse> success, Action<ICsqWebResponse> fail, int id, ServerConfig options = null) { var request = new CsqWebRequest(url); ServerConfig.Apply(options, request); request.Id = id; request.Async = true; var mrEvent = request.GetAsync(success, fail); AsyncEvents.Add(mrEvent); }
/// <summary> /// Apply these options to a web request. /// </summary> /// /// <param name="options"> /// The options. /// </param> /// <param name="request"> /// The CsqWebRequest object to apply the options to. /// </param> public static void Apply(ServerConfig options, ICsqWebRequest request) { var opts = Merge(options); if (opts.Timeout != null) { request.Timeout = (int)Math.Floor(opts.Timeout.TotalMilliseconds); } if (opts.UserAgent != null) { request.UserAgent = opts.UserAgent; } }
/// <summary> /// Creates a new DOM from an HTML file. /// </summary> /// /// <param name="url"> /// The URL of the remote server. /// </param> /// <param name="options"> /// The options to use when creating the reqest. /// </param> /// /// <returns> /// A CQ object composed from the HTML response from the server. /// </returns> public static CQ CreateFromUrl(string url, ServerConfig options=null) { CsqWebRequest request = new CsqWebRequest(url); request.Options = options; var httpRequest = request.GetWebRequest(); var response = httpRequest.GetResponse(); var responseStream = response.GetResponseStream(); var encoding = CsqWebRequest.GetEncoding(response); return CQ.CreateDocument(responseStream,encoding); }
/// <summary> /// Merge any non-null options into a new options object. /// </summary> /// /// <param name="options"> /// The options /// </param> /// /// <returns> /// A new ServerConfig object /// </returns> public static ServerConfig Merge(ServerConfig options) { ServerConfig config = ServerConfig.Default; if (options != null) { if (options.UserAgent != null) { config.UserAgent = options.UserAgent; } if (options.Timeout != null) { config.Timeout = options.Timeout; } } return config; }
public void RandomUrls() { ServerConfig options = new ServerConfig { TimeoutSeconds=5 }; int outer = totalTests/simultaneousThreads; var list = GetRandomUrls(totalTests); List<string> successList = new List<string>(); List<string> failList = new List<string>(); int cur=0; for (int i = 0; i < outer; i++) { IPromise[] promises = new IPromise[simultaneousThreads]; List<string> active = new List<string>(); for (int j = 0; j < simultaneousThreads; j++) { string url = list[cur++]; active.Add(url); promises[j] = CQ.CreateFromUrlAsync(url, options).Then((success) => { successList.Add(FormatResult(success)); active.Remove(success.Url); },(fail) => { failList.Add(FormatResult(fail)); active.Remove(fail.Url); }); } if (!AsyncWebRequestManager.WaitForAsyncEvents(10000)) { AsyncWebRequestManager.CancelAsyncEvents(); foreach (var item in active) { failList.Add(item + ": aborted"); } } } Debug.WriteLine(FormatTestOutput(successList,failList)); }
/// <summary> /// Start an asynchronous request to an HTTP server. /// </summary> /// /// <param name="url"> /// The URL of the remote server. /// </param> /// <param name="callbackSuccess"> /// A delegate to invoke upon successful completion of the request. /// </param> /// <param name="callbackFail"> /// A delegate to invoke upon failure. /// </param> /// <param name="options"> /// Options to use when creating the request. /// </param> /// /// <returns> /// A unique identifier which will be passed through to the response and can be used to assocate /// a response with this request. /// </returns> public static int CreateFromUrlAsync(string url, Action<ICsqWebResponse> callbackSuccess, Action<ICsqWebResponse> callbackFail=null, ServerConfig options = null) { return AsyncWebRequestManager.StartAsyncWebRequest(url,callbackSuccess,callbackFail,options); }
/// <summary> /// Start an asynchronous request to an HTTP server, returning a promise that will resolve when /// the request is completed or rejected. /// </summary> /// /// <param name="url"> /// The URL of the remote server /// </param> /// <param name="options"> /// The options to use when creating the reqest /// </param> /// /// <returns> /// A promise that resolves when the request completes /// </returns> public static IPromise<ICsqWebResponse> CreateFromUrlAsync(string url, ServerConfig options = null) { var deferred = When.Deferred<ICsqWebResponse>(); int uniqueID = AsyncWebRequestManager.StartAsyncWebRequest(url, deferred.Resolve, deferred.Reject, options); return deferred; }
/// <summary> /// Start an async request, and return a unique ID that identifies it. /// </summary> /// /// <param name="url"> /// The URL of the remote server. /// </param> /// <param name="success"> /// A delegate to invoke upon successful completion of the request. /// </param> /// <param name="fail"> /// A delegate to invoke when a request fails. /// </param> /// <param name="id"> /// The identifier. /// </param> /// <param name="options"> /// Options to be used when creating this request. If not provided, the default options will be /// used. /// </param> public static void StartAsyncWebRequest(string url, Action <ICsqWebResponse> success, Action <ICsqWebResponse> fail, int id, ServerConfig options = null) { var request = new CsqWebRequest(url); ServerConfig.Apply(options, request); request.Id = id; request.Async = true; var mrEvent = request.GetAsync(success, fail); AsyncEvents.Add(mrEvent); }