public async Task TestFishUser() { HttpClientExt.ShareCookie = new CookieContainer(); HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Pages/Demo/TestAuthorize/Fish.aspx", Method = "GET" }; int stateCode = await option.GetStatusCode(); Assert.AreEqual(403, stateCode); // 登录 await Login("fish11"); // 登录后再试一次 stateCode = await option.GetStatusCode(); Assert.AreEqual(403, stateCode); // 用Fish登录后再试一次 await Login("Fish"); stateCode = await option.GetStatusCode(); Assert.AreEqual(200, stateCode); // 注销用户 await Logout(); HttpClientExt.ShareCookie = null; }
public async Task TestAllUser() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Pages/Demo/TestAuthorize/AllUser.aspx", Method = "GET" }; string actual = await HttpClient.SendAsync<string>(option); Assert.IsTrue(actual.IndexOf("此页面可由所有用户访问。") > 0); }
public void Test2() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/mvc-routing/ns/TestAutoAction/Md5", Data = new { input = "Fish Li" } }; string actual = HttpClient.Send<string>(option); string expected = "44D2D9635ED5CDEA2A858CD7A1CC2B0C"; Assert.AreEqual(expected, actual); }
public async Task Test2() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/ajax/pk/DemoPk/Add.cspx", Method = "POST", Data = new { a = 1, b = 2 } }; string actual = await HttpClient.SendAsync<string>(option); string expected = "3"; Assert.AreEqual(expected, actual); }
public void Test3() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/mvc-routing/ns/TestAutoAction/Sha1", Data = new { input = "Fish Li" } }; string actual = HttpClient.Send<string>(option); string expected = "A6DCC78B685D0CEA701CA90A948B9295F3685FDF"; Assert.AreEqual(expected, actual); }
public void Test1() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/mvc-routing/ns/TestAutoAction/Base64", Data = new { input = "Fish Li" } }; string actual = HttpClient.Send<string>(option); string expected = "RmlzaCBMaQ=="; Assert.AreEqual(expected, actual); }
public async Task TestAutoFindAction2() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/ns/TestAutoAction/submit.aspx", Method = "POST", Data = new { Md5 = "yes", input = "Fish Li" } }; string actual = await HttpClient.SendAsync<string>(option); string expected = "44D2D9635ED5CDEA2A858CD7A1CC2B0C"; Assert.AreEqual(expected, actual); }
public async Task TestAutoFindAction1() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/ns/TestAutoAction/submit.aspx", Method = "POST", Data = new { Base64 = "yes", input = "Fish Li" } }; string actual = await HttpClient.SendAsync<string>(option); string expected = "RmlzaCBMaQ=="; Assert.AreEqual(expected, actual); }
public async Task TestSubmitNumber() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/ns/Demo/TestEnum.aspx?submit=submit", Data = new { week = "2" }, Method = "GET" }; string actual = await HttpClient.SendAsync<string>(option); string expected = "Tuesday"; Assert.AreEqual(expected, actual); }
public async Task TestMd5() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/ns/Demo/GetMd5.aspx", Data = new { input = "Fish Li" }, Method = "GET" }; string actual = await HttpClient.SendAsync<string>(option); string expected = "44d2d9635ed5cdea2a858cd7a1cc2b0c"; Assert.AreEqual(expected, actual); }
public async Task TestAutoFindAction3() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/ns/TestAutoAction/submit.aspx", Method = "POST", Data = new { Sha1 = "yes", input = "Fish Li" } }; string actual = await HttpClient.SendAsync<string>(option); string expected = "A6DCC78B685D0CEA701CA90A948B9295F3685FDF"; Assert.AreEqual(expected, actual); }
public async Task TestNamespace_2() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/Fish.BB.Test/Add.aspx", Data = new { a = 2, b = 3 }, Method = "GET" }; string actual = await HttpClient.SendAsync<string>(option); string expected = "15"; Assert.AreEqual(expected, actual); }
public async Task TestNamespace_3() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/DEMO.Controllers.TestTask.TaskDemoService/Add.aspx", Data = new { a = 2, b = 3 }, Method = "GET" }; string actual = await HttpClient.SendAsync<string>(option); string expected = "105"; Assert.AreEqual(expected, actual); }
public async Task Test2() { System.IO.FileInfo file1 = new System.IO.FileInfo("Newtonsoft.Json.dll"); System.IO.FileInfo file2 = new System.IO.FileInfo("ClownFish.Mvc.dll"); HttpOption option = new HttpOption { Method = "POST", Url = "http://www.fish-mvc-demo.com/Ajax/ns/UploadFile/Test1.aspx", Data = new { a = file1, b = file2, c = 2, d = 5 } }; string actual = await HttpClient.SendAsync<string>(option); //{ // "file1": { // "name": "E:\\ProjectFiles\\ClownFish.Mvc\\ClownFish.Mvc3\\Test\\TestApplication1\\bin\\Newtonsoft.Json.dll", // "lenght": 430080 // }, // "file2": { // "name": "E:\\ProjectFiles\\ClownFish.Mvc\\ClownFish.Mvc3\\Test\\TestApplication1\\bin\\ClownFish.Mvc.dll", // "lenght": 165888 // }, // "sum": 595975 //} string expected = (2 + 5 + file1.Length + file2.Length).ToString(); Assert.IsTrue(actual.IndexOf(expected) > 0); HttpOption option2 = new HttpOption { Method = "POST", Url = "http://www.fish-mvc-demo.com/Ajax/ns/UploadFile/Test2.aspx", Data = new { a = file1, b = file2, c = 2, d = 5 } }; string actual2 = await option2.SendAsync<string>(); Assert.AreEqual(actual2, actual); }
public async Task<PageResult> GetWebsiteTitles(HttpContext context, string urls) { context.WriteHeader("action-before-await"); context.WriteHeader("---------------------------------------------------------"); TestTaskPageViewData viewData = new TestTaskPageViewData(); viewData.Input = urls; if( string.IsNullOrEmpty(urls) ) { viewData.Input = @" http://cn.bing.com/ http://email.163.com/ http://www.aliyun.com/ http://baike.baidu.com/ http://www.dingtalk.com/ http://www.taobao.com/ http://www.tmall.com/"; } else { string[] urlArray = urls.SplitTrim('\r', '\n'); List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(urlArray.Length); foreach( string url in urlArray ) { try { HttpOption option = new HttpOption { Url = url, Timeout = 2000 }; string text = await HttpClient.SendAsync<string>(option); context.WriteHeader("HttpClient.SendAsync<string>(url)--after-await"); string title = GetHtmlTitle(text) ?? "-----------------"; list.Add(new KeyValuePair<string, string>(url, title)); } catch( Exception ex ) { list.Add(new KeyValuePair<string, string>(url, ex.GetBaseException().Message)); } viewData.Result = list; } } context.WriteHeader("action-after-await-return"); return new PageResult(null, viewData); }
public async Task Test3() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/ajax/pk/DemoPk/AddCustomer.cspx", Method = "POST", Data = new { Address = "武汉", Age = 20, Email = "*****@*****.**", Name = "abc", Tel = "12345678" } }; string actual = await HttpClient.SendAsync<string>(option); string expected = @" <Name>abc</Name> <Age>20</Age> <Address>武汉</Address> <Tel>12345678</Tel> <Email>[email protected]</Email> </Customer>".Trim(); Assert.IsTrue(actual.EndsWith(expected)); }
private void Init(HttpOption option) { if (option == null) { throw new ArgumentNullException("option"); } if (_option != null) { throw new InvalidOperationException("当前HttpClient只能发送一次请求。"); } option.CheckInput(); option.SetPostData(); _option = option; // 重新计算请求参数 ProcessOption(); // 创建请求实例 this.Request = CreateRequest(); // 发送请求前事件 EventHandler <BeforeSendRequestEventArgs> handler = OnBeforeSendRequest; if (handler != null) { handler(this, new BeforeSendRequestEventArgs { Request = this.Request, Option = option }); } }
private async Task Test4Internal(string url) { string input = @" Address=武汉 Address=上海 Address=武汉 Address= Address= Age=20 Age=20 Age=20 Age= Age= [email protected] Email= [email protected] Email= Email= Name=A1 Name=A2 Name=A3 Name= Name=A5 Tel=12345678 Tel=22222222 Tel= Tel= Tel=12345678 batchAddCustomer=保存客户资料"; HttpOption option = new HttpOption { Url = url, Method = "POST", Data = StringToFormDataCollection(input) }; string actual = await HttpClient.SendAsync<string>(option); string expected = @" <Customer> <Name>A1</Name> <Age>20</Age> <Address>武汉</Address> <Tel>12345678</Tel> <Email>[email protected]</Email> </Customer> <Customer> <Name>A2</Name> <Age>20</Age> <Address>上海</Address> <Tel>22222222</Tel> <Email /> </Customer> </ArrayOfCustomer>".Trim(); Assert.IsTrue(actual.EndsWith(expected)); }
/// <summary> /// 根据指定的HttpRequestOption参数,用【同步】方式发起一次HTTP请求 /// </summary> /// <typeparam name="T">返回值的类型参数</typeparam> /// <param name="option">HttpRequestOption的实例,用于描述请求参数</param> /// <returns>返回服务端的调用结果,并转换成指定的类型</returns> public static T Send <T>(this HttpOption option) { return(HttpClient.Send <T>(option)); }
public async Task TestCustomerType() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/ns/Demo2/TestCustomerType.aspx", Data = new FormDataCollection() .Add("customer.Name", "name----1") .Add("customer.Tel", "tel----1") .Add("salesman.Name", "name----2") .Add("salesman.Tel", "tel----2"), Method = "POST" }; string actual = await HttpClient.SendAsync<string>(option); string expected = @" customer.Name = name----1 customer.Tel = tel----1 salesman.Name = name----2 salesman.Name = tel----2".Trim(); Assert.AreEqual(expected, actual); }
/// <summary> /// 根据指定的HttpRequestOption参数,用【异步】方式发起一次HTTP请求 /// </summary> /// <typeparam name="T">返回值的类型参数</typeparam> /// <param name="option">HttpRequestOption的实例,用于描述请求参数</param> /// <returns>返回服务端的调用结果,并转换成指定的类型</returns> public static Task <T> SendAsync <T>(this HttpOption option) { return(HttpClient.SendAsync <T>(option)); }
/// <summary> /// 根据指定的HttpRequestOption参数,用【同步】方式发起一次HTTP请求 /// </summary> /// <typeparam name="T">返回值的类型参数</typeparam> /// <param name="option">HttpRequestOption的实例,用于描述请求参数</param> /// <returns>返回服务端的调用结果,并转换成指定的类型</returns> public static T Send <T>(HttpOption option) { using (HttpClient client = ObjectFactory.New <HttpClient>()) { return(client.GetResult <T>(option)); } }
/// <summary> /// 根据原始请求信息文本构建 HttpRequestOption 对象(格式可参考Fiddler的Inspectors标签页内容) /// </summary> /// <param name="text"></param> /// <returns></returns> public static HttpOption FromRawText(string text) { // 示例数据: //POST http://www.fish-mvc-demo.com/Ajax/ns/TestAutoAction/submit.aspx HTTP/1.1 //Host: localhost:37376 //User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0 //Accept: */* //Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3 //Accept-Encoding: gzip, deflate //Content-Type: application/x-www-form-urlencoded; charset=UTF-8 //X-Requested-With: XMLHttpRequest //Referer: http://www.fish-mvc-demo.com/Pages/Demo/TestAutoFindAction.htm //Content-Length: 72 //Cookie: hasplmlang=_int_; LoginBy=productKey; PageStyle=Style2; //Connection: keep-alive //Pragma: no-cache //Cache-Control: no-cache //input=Fish+Li&Base64=%E8%BD%AC%E6%8D%A2%E6%88%90Base64%E7%BC%96%E7%A0%81 if( string.IsNullOrEmpty(text) ) throw new ArgumentNullException("text"); HttpOption option = new HttpOption(); using( StringReader reader = new StringReader(text.Trim()) ) { string firstLine = reader.ReadLine(); int p1 = firstLine.IndexOf(' '); int p2 = firstLine.LastIndexOf(' '); if( p1 < 0 || p1 == p2 ) throw new ArgumentException("不能识别的请求文本格式。"); option.Method = firstLine.Substring(0, p1); // 不使用HTTP协议版本,只做校验。 string httpVersion = firstLine.Substring(p2 + 1); if( httpVersion.StartsWith("HTTP/") == false ) throw new ArgumentException("不能识别的请求文本格式。"); option.Url = firstLine.Substring(p1 + 1, p2 - p1 - 1); string line = null; while( (line = reader.ReadLine()) != null ) { if( line.Length > 0 ) { // 处理请求头 int p3 = line.IndexOf(':'); if( p3 > 1 ) { string name = line.Substring(0, p3); string value = line.Substring(p3 + 2); option.Headers.Add(name, value); } else throw new ArgumentException("不能识别的请求文本格式。"); } else break; } // 请求体数据 string postText = reader.ReadToEnd(); if( string.IsNullOrEmpty(postText) == false ) option.Data = postText; } return option; }
private async Task Login(string username, string rightNo = null) { HttpOption option2 = new HttpOption { Url = "http://www.fish-mvc-demo.com/user/Login.aspx", Method = "POST", Data = new { username = username, rightNo = rightNo } }; int stateCode2 = await option2.GetStatusCode(); Assert.AreEqual(200, stateCode2); }
private async Task Logout() { HttpOption option9 = new HttpOption { Url = "http://www.fish-mvc-demo.com/user/Logout.aspx", Method = "POST", Data = new { Logout = "注销" } }; int stateCode9 = await option9.GetStatusCode(); Assert.AreEqual(200, stateCode9); }
/// <summary> /// 根据指定的HttpRequestOption参数,用【异步】方式发起一次HTTP请求 /// </summary> /// <typeparam name="T">返回值的类型参数</typeparam> /// <param name="option">HttpRequestOption的实例,用于描述请求参数</param> /// <returns>返回服务端的调用结果,并转换成指定的类型</returns> public static async Task <T> SendAsync <T>(HttpOption option) { using (HttpClient client = ObjectFactory.New <HttpClient>()) { return(await client.GetResultAsync <T>(option)); } }
/// <summary> /// 根据原始请求信息文本构建 HttpRequestOption 对象(格式可参考Fiddler的Inspectors标签页内容) /// </summary> /// <param name="text"></param> /// <returns></returns> public static HttpOption FromRawText(string text) { // 示例数据: //POST http://www.fish-mvc-demo.com/Ajax/ns/TestAutoAction/submit.aspx HTTP/1.1 //Host: localhost:37376 //User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0 //Accept: */* //Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3 //Accept-Encoding: gzip, deflate //Content-Type: application/x-www-form-urlencoded; charset=UTF-8 //X-Requested-With: XMLHttpRequest //Referer: http://www.fish-mvc-demo.com/Pages/Demo/TestAutoFindAction.htm //Content-Length: 72 //Cookie: hasplmlang=_int_; LoginBy=productKey; PageStyle=Style2; //Connection: keep-alive //Pragma: no-cache //Cache-Control: no-cache //input=Fish+Li&Base64=%E8%BD%AC%E6%8D%A2%E6%88%90Base64%E7%BC%96%E7%A0%81 if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); } HttpOption option = new HttpOption(); using (StringReader reader = new StringReader(text.Trim())) { string firstLine = reader.ReadLine(); int p1 = firstLine.IndexOf(' '); int p2 = firstLine.LastIndexOf(' '); if (p1 < 0 || p1 == p2) { throw new ArgumentException("不能识别的请求文本格式。"); } option.Method = firstLine.Substring(0, p1); // 不使用HTTP协议版本,只做校验。 string httpVersion = firstLine.Substring(p2 + 1); if (httpVersion.StartsWith("HTTP/") == false) { throw new ArgumentException("不能识别的请求文本格式。"); } option.Url = firstLine.Substring(p1 + 1, p2 - p1 - 1); string line = null; while ((line = reader.ReadLine()) != null) { if (line.Length > 0) { // 处理请求头 int p3 = line.IndexOf(':'); if (p3 > 1) { string name = line.Substring(0, p3); string value = line.Substring(p3 + 2); option.Headers.Add(name, value); } else { throw new ArgumentException("不能识别的请求文本格式。"); } } else { break; } } // 请求体数据 string postText = reader.ReadToEnd(); if (string.IsNullOrEmpty(postText) == false) { option.Data = postText; } } return(option); }