public GetAsync ( Action |
||
success | Action |
/// A delegate that will be invoked with the response data structure upon successful resolution /// of the request. /// |
fail | Action |
/// A delegate that will be invoked with the response data structure upon failure. /// |
Результат |
/// <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); }
public void GetHtmlAsyncTimeout() { var creator = new Mocks.MockWebRequestCreator(); creator.ResponseTime = 1000; creator.ResponseStream = GetMemoryStream("<div>Test</div>"); var request = new CsqWebRequest("http://test.com", creator); request.Options = new ServerConfig { Timeout = TimeSpan.FromMilliseconds(500), UserAgent = "test" }; bool? done = null; request.GetAsync((r) => { done = true; }, (r) => { done = false; }); while (done == null) ; Assert.IsFalse((bool)done); creator.ResponseTime = 300; request = new CsqWebRequest("http://test.com", creator); done = null; request.GetAsync((r) => { done = true; }, (r) => { done = false; }); while (done == null) ; Assert.IsTrue((bool)done); }
/// <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); }
public void Utf8NoContentType() { var creator = new Mocks.MockWebRequestCreator(); creator.CharacterSet = "ISO-8859-1"; creator.ResponseStream = GetMemoryStream(TestHtml("arabic"), new UTF8Encoding(false)); CsqWebRequest request = new CsqWebRequest("http://test.com", creator); // remove the content type header var html = ReplaceCharacterSet(request.Get()); var dom = CQ.CreateDocument(html); Assert.AreNotEqual(arabicExpected, dom["h1"].Text()); //test synchronous: this is the code that CreateFromURL uses creator.CharacterSet = null; request = new CsqWebRequest("http://test.com", creator); var httpRequest = request.GetWebRequest(); var response = httpRequest.GetResponse(); var responseStream = response.GetResponseStream(); var encoding = CsqWebRequest.GetEncoding(response); var dom2 = CQ.CreateDocument(responseStream, encoding); Assert.AreEqual(arabicExpected, dom2["h1"].Text()); // Test async version now request = new CsqWebRequest("http://test.com", creator); bool? done=null; CQ dom3=null; request.GetAsync((r) => { dom3 = r.Dom; done = true; }, (r)=>{ done = false; }); while (done == null) ; Assert.IsTrue((bool)done); Assert.AreEqual(arabicExpected, dom3["h1"].Text()); }
public static void StartAsyncWebRequest(string url, Action<ICsqWebResponse> callback, object id=null) { var request = new CsqWebRequest(url); request.Id = id; request.Async = true; var mrEvent = request.GetAsync(callback); AsyncEvents.Add(mrEvent); }
public void Utf8NoContentType() { var creator = new Mocks.MockWebRequestCreator(); creator.CharacterSet = "ISO-8859-1"; creator.ResponseStream = GetMemoryStream(TestHtml("arabic"), new UTF8Encoding(false)); CsqWebRequest request = new CsqWebRequest("http://test.com", creator); // remove the content type header var html = request.Get(); var start = html.IndexOf(@"<meta http-equiv=""Content-Type"""); var end = html.IndexOf(">",start); html = html.Substring(0,start)+html.Substring(end+1); var dom = CQ.CreateDocument(html); var expected = @"البابا: اوقفوا ""المجزرة"" في سوريا قبل ان تتحول البلاد الى ""أطلال"""; Assert.AreNotEqual(expected, dom["h1"].Text()); //test synchronous: this is the code that CreateFromURL uses request = new CsqWebRequest("http://test.com", creator); var httpRequest = request.GetWebRequest(); var response = httpRequest.GetResponse(); var responseStream = response.GetResponseStream(); var encoding = CsqWebRequest.GetEncoding(response); var dom2 = CQ.CreateDocument(responseStream, encoding); Assert.AreEqual(expected, dom2["h1"].Text()); // Test async version now request = new CsqWebRequest("http://test.com", creator); bool? done=null; CQ dom3=null; request.GetAsync((r) => { dom3 = r.Dom; done = true; }, (r)=>{ done = false; }); while (done == null) ; Assert.IsTrue((bool)done); Assert.AreEqual(expected, dom3["h1"].Text()); }