public async Task Test2() { System.IO.FileInfo file1 = new System.IO.FileInfo("Newtonsoft.Json.dll"); System.IO.FileInfo file2 = new System.IO.FileInfo("ClownFish.Web.dll"); HttpOption option = new HttpOption { Method = "POST", Url = "http://www.fish-web-demo.com/api/ns/UploadFile/Test1.aspx", Data = new { a = file1, b = file2, c = 2, d = 5 } }; string actual = await option.SendAsync <string>(); //{ // "file1": { // "name": "E:\\ProjectFiles\\ClownFish.Web\\ClownFish.Web3\\Test\\ClownFish.TestApplication1\\bin\\Newtonsoft.Json.dll", // "lenght": 430080 // }, // "file2": { // "name": "E:\\ProjectFiles\\ClownFish.Web\\ClownFish.Web3\\Test\\ClownFish.TestApplication1\\bin\\ClownFish.Web.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-web-demo.com/api/ns/UploadFile/Test2.aspx", Data = new { a = file1, b = file2, c = 2, d = 5 } }; string actual2 = await option2.SendAsync <string>(); Assert.AreEqual(actual2, actual); }
private static void DownloadFile(string filename) { string website = ConfigurationManager.AppSettings["ServiceWebsite"]; HttpOption option = new HttpOption { Method = "POST", Url = website.TrimEnd('/') + "/ajax/scan/File/GetFile.ppx", Data = new { filename = filename } }; string text = option.GetResult(); s_configFileTextDict[filename] = text; }
/// <summary> /// Constructs an HTTP connection. /// </summary> /// <param name="options">The connection options.</param> /// <param name="cbContentMax">Maximum allowed content size or <b>-1</b> to disable checking.</param> /// <param name="perfBytesRecv">Performance counter to receive updates about bytes received (or <c>null</c>).</param> /// <param name="perfBytesSent">Performance counter to receive updates about bytes sent (or <c>null</c>).</param> public HttpConnection(HttpOption options, int cbContentMax, PerfCounter perfBytesRecv, PerfCounter perfBytesSent) { this.id = AllocID(); this.cbContentMax = cbContentMax; this.sock = null; this.ipAddress = IPAddress.Any; this.options = options; this.timedOut = false; this.onRequestSent = new AsyncCallback(OnRequestSent); this.onResponseRecv = new AsyncCallback(OnResponseReceived); this.perfBytesRecv = perfBytesRecv; this.perfBytesSent = perfBytesSent; }
public void 发送HTTP请求_指定请求头() { string text = new HttpOption { Method = "POST", Url = "http://www.fish-web-demo.com/test1.aspx", Data = new { id = 2, name = "abc" }, Headers = new Dictionary <string, string>() { { "X-Requested-With", "XMLHttpRequest" }, { "User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64)" } } }.GetResult(); }
internal static void SendData(object data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } HttpOption httpOption = CreateHttpOption(data); Retry retry = Retry.Create(s_retryCount, s_retryWaitMillisecond); httpOption.GetResult(retry); }
public async Task TestNamespace_3() { HttpOption option = new HttpOption { Url = "http://www.fish-web-demo.com/Ajax/DEMO.Controllers.TestTask.TaskDemoService/Add.aspx", Data = new { a = 2, b = 3 }, Method = "GET" }; string actual = await option.SendAsync <string>(); string expected = "105"; 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 option.SendAsync <string>(); string expected = "Tuesday"; Assert.AreEqual(expected, actual); }
public async Task TestAutoFindAction1() { HttpOption option = new HttpOption { Url = "http://www.fish-web-demo.com/api/ns/TestAutoAction/submit.aspx", Method = "POST", Data = new { Base64 = "yes", input = "Fish Li" } }; string actual = await option.SendAsync <string>(); string expected = "RmlzaCBMaQ=="; Assert.AreEqual(expected, actual); }
public async static Task <int> GetStatusCode(this HttpOption option) { try { await HttpClient.SendAsync <string>(option); return(200); } catch (WebException ex) { return(ex.GetStatusCode()); } catch (Exception) { return(500); } }
public async Task TestSubmitName() { HttpOption option = new HttpOption { Url = "http://www.fish-web-demo.com/api/ns/Demo1/TestEnum.aspx?submit=submit", Data = new { week = "Thursday" }, Method = "GET" }; string actual = await option.SendAsync <string>(); string expected = "Thursday"; 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 option.SendAsync <string>(); string expected = "3"; Assert.AreEqual(expected, actual); }
public async Task TestAutoFindAction2() { HttpOption option = new HttpOption { Url = "http://www.fish-web-demo.com/api/ns/TestAutoAction/submit.aspx", Method = "POST", Data = new { Md5 = "yes", input = "Fish Li" } }; string actual = await option.SendAsync <string>(); string expected = "44D2D9635ED5CDEA2A858CD7A1CC2B0C"; Assert.AreEqual(expected, actual); }
public async Task TestAutoFindAction3() { HttpOption option = new HttpOption { Url = "http://www.fish-web-demo.com/api/ns/TestAutoAction/submit.aspx", Method = "POST", Data = new { Sha1 = "yes", input = "Fish Li" } }; string actual = await option.SendAsync <string>(); string expected = "A6DCC78B685D0CEA701CA90A948B9295F3685FDF"; Assert.AreEqual(expected, actual); }
public async Task TestMd5() { HttpOption option = new HttpOption { Url = "http://www.fish-web-demo.com/api/ns/Demo1/GetMd5.aspx", Data = new { input = "Fish Li" }, Method = "GET" }; string actual = await option.SendAsync <string>(); string expected = "44d2d9635ed5cdea2a858cd7a1cc2b0c"; Assert.AreEqual(expected, actual); }
public async Task TestNamespace_2() { HttpOption option = new HttpOption { Url = "http://www.fish-web-demo.com/Ajax/Fish.BB.Test/Add.aspx", Data = new { a = 2, b = 3 }, Method = "GET" }; string actual = await option.SendAsync <string>(); string expected = "15"; Assert.AreEqual(expected, actual); }
public async Task TestNamespace_1() { HttpOption option = new HttpOption { Url = "http://www.fish-mvc-demo.com/Ajax/Fish.AA.Test/Add.aspx", Data = new { a = 2, b = 3 }, Method = "GET" }; string actual = await HttpClient.SendAsync <string>(option); string expected = "5"; Assert.AreEqual(expected, actual); }
public async Task Test3() { HttpOption option = HttpOption.FromRawText(@" POST http://www.fish-mvc-demo.com/Ajax/ns/TestFile/Sum.aspx HTTP/1.1 Content-Type: application/x-www-form-urlencoded numbers=1&numbers=2&numbers=3&numbers=4&numbers=5"); string actual = await option.SendAsync <string>(); string expected = "15"; Assert.AreEqual(expected, actual); }
public void 发送HTTP请求_读取响应头() { string header1 = null; string header2 = null; string text = new HttpOption { Method = "POST", Url = "http://www.fish-web-demo.com/test1.aspx", Data = new { id = 2, name = "abc" }, ReadResponseAction = rep => { // 注意这里 header1 = rep.Headers["x-header1"]; header2 = rep.Headers["x-header2"]; } }.GetResult(); }
private void SetCookie(HttpOption option) { // 如果接收到COOKIE,就记录下来 option.ReadResponseAction = SaveNewCookie; if (_setCookies.Count == 0) { return; } StringBuilder cookieBuilder = new StringBuilder(); // 取出脚本上指定的COOKIE string currentCookie = option.Headers["Cookie"]; if (string.IsNullOrEmpty(currentCookie) == false) { // 移除原请求头 option.Headers.Remove("Cookie"); // 拆分当前请求的COOKIE头字符串 List <NameValue> c2 = StringExtensions.SplitString(currentCookie, ';', '='); // 始终以服务端创建的COOKIE为准 foreach (var c in c2) { var x = _setCookies.FirstOrDefault(a => a.Name.EqualsIgnoreCase(c.Name)); if (x == null) { cookieBuilder.Append(c.Name).Append("=").Append(c.Value).Append("; "); } } } foreach (var c in _setCookies) { cookieBuilder.Append(c.Name).Append("=").Append(c.Value).Append("; "); } if (cookieBuilder.Length > 0) { cookieBuilder.Remove(cookieBuilder.Length - 2, 2); } // 重新设置请求头 option.Headers.Add("Cookie", cookieBuilder.ToString()); }
private static void DownloadTaskFile(int id) { string filename = null; string website = ConfigurationManager.AppSettings["ServiceWebsite"]; HttpOption option = new HttpOption { Method = "POST", Url = website.TrimEnd('/') + "/ajax/scan/File/GetTaskFile.ppx", Data = new { id = id } }; option.ReadResponseAction = x => filename = System.Web.HttpUtility.UrlDecode(x.GetResponseHeader("x-filename")); string text = option.GetResult(); s_configFileTextDict[filename] = text; }
static void SendRequest() { try { HttpOption option = new HttpOption { Url = "http://localhost:50456/hello/ClownFish-HttpServer/demo-ccc/Now.aspx" //Url = "http://localhost:50456/hello/ClownFish-HttpServer/demo-ccc/Now.aspx?sleepMillisecondsTimeout=90" }; string response = option.GetResult(); } catch (RemoteWebException webException) { Console.WriteLine(webException.ResponseText); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public async Task Test_BinaryResult() { string text = "复制可执行的SQL"; HttpOption option = HttpOption.FromRawText(@" POST http://www.fish-web-demo.com/api/ns/Demo1/TestBinaryResult.aspx HTTP/1.1 Content-Type: application/x-www-form-urlencoded s=" + System.Web.HttpUtility.UrlEncode(text)); byte[] actual = await option.GetResultAsync <byte[]>(); byte[] expected = Encoding.UTF8.GetBytes(text); actual.IsEqual(expected); }
public void 发送HTTP请求_POST_提交表单数据_支持文件上传() { string text = new HttpOption { Method = "POST", Url = "http://www.fish-web-demo.com/test1.aspx", Data = new { id = 2, // 如果 Data 属性包含 FileInfo 或者 HttpFile 类型的属性值,就认为是上传文件 file1 = new FileInfo(@"ClownFish.Base.xml"), file2 = new FileInfo(@"ClownFish.Base.dll") } }.GetResult(); // 说明: // 1、上传文件时,不要为HttpOption指定 Format 属性,因为默认值就是支持上传文件。 // 2、服务端如果使用ClownFish.Web 可以使用 HttpFile 来接收上传文件。 }
public async static Task <int> GetStatusCode(this HttpOption option) { try { await option.SendAsync <string>(); return(200); } catch (RemoteWebException remoteWebException) { return(remoteWebException.GetStatusCode()); } catch (WebException ex) { return(ex.GetStatusCode()); } catch (Exception) { return(500); } }
public void Execute(TaskContext context, TaskAction action) { string website = ConfigurationManager.AppSettings["ServiceWebsite"]; if (string.IsNullOrEmpty(website)) { throw new ConfigurationErrorsException("ServiceWebsite没有配置。"); } TotalResult totalResult = context.TotalResult; // 按业务单元和扫描类别分组小计 //CalculateSubTotal(totalResult); context.ConsoleWrite("ExecSubTotalResult OK"); context.ConsoleWrite("\r\n任务结束时间:" + DateTime.Now.ToTimeString()); // 获取控件台的所有输出内容 totalResult.ConsoleText = context.OutputText; // 为了防止提交的数据过大,所以采用压缩的方式提交数据(大约可压缩10倍), string json = totalResult.ToJson(); string data = CompressHelper.GzipCompress(json); HttpOption option = new HttpOption { Method = "POST", Url = website.TrimEnd('/') + "/ajax/scan/Upload/UploadResult.ppx", Data = new { base64 = data, branchId = context.Branch.Id } }; option.Headers.Add("authentication-key", ConfigurationManager.AppSettings["authentication-key"]); option.Headers.Add("app-version", SpecChecker.CoreLibrary.Config.JobManager.AppVersion); string responseText = option.GetResult(); if (responseText == "200") { context.ConsoleWrite("UploadResultTask OK"); } else { context.ConsoleWrite("\r\n上传结果出现异常:##################"); context.ConsoleWrite(responseText); } }
private void ExecuteReqest(RequestInfo request, HttpOption option, int index) { request.Result = new ExecuteResult(); ScriptExecuteEventArgs e1 = new ScriptExecuteEventArgs { Index = index, Option = option, Request = request }; if (this.BeforeExecute != null) { this.BeforeExecute(this, e1); } Stopwatch watch = Stopwatch.StartNew(); try { string result = option.Send <string>(); watch.Stop(); request.Result.Response = result; request.Result.Time = watch.Elapsed; if (this.AfterExecute != null) { this.AfterExecute(this, e1); } } catch (Exception ex) { watch.Stop(); _errorCount++; ScriptExecuteErrorEventArgs e2 = new ScriptExecuteErrorEventArgs { Index = index, Option = option, Request = request, Exception = ex }; ProcessException(ex, request, e2); } finally { request.Result.RequestText = option.ToRawText(); } }
/// <summary> /// 发送HTTP请求 /// </summary> /// <param name="data"></param> public virtual void SendData(object data) { if (data == null) { return; } HttpOption httpOption = CreateHttpOption(data); if (httpOption == null) { return; } Retry retry = Retry.Create(_retryCount, _retryWaitMillisecond); httpOption.GetResult(retry); }
private HttpOption CreateHttpOption(RequestInfo request, bool supportWindowsAuth, string userAgent) { HttpOption option = HttpOption.FromRawText(request.Text); option.Headers.Remove("X-Mysoft-Profiler"); // 移队Fiddler插件中增加的监控头,避免返回无用信息 option.Headers.Remove("Host"); // 这个头没什么用,反而可能与网址参数不一样 if (string.IsNullOrEmpty(userAgent) == false) { option.Headers.Remove("User-Agent"); option.Headers.Add("User-Agent", userAgent); } if (supportWindowsAuth) // 支持Windows身份认证 { option.Credentials = CredentialCache.DefaultCredentials; } // 重新计算请求地址 if (string.IsNullOrEmpty(_currentExecuteInfo.TargetSite) == false) { option.Url = request.RealUrl = _currentExecuteInfo.TargetSite + request.RelativeUrl; string referer = option.Headers["Referer"]; if (string.IsNullOrEmpty(referer) == false) { referer = referer.Replace(request.SiteRoot, _currentExecuteInfo.TargetSite); option.Headers.Remove("Referer"); option.Headers.Add("Referer", referer); } } else { request.RealUrl = request.SiteRoot + request.RelativeUrl; } return(option); }
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 option.SendAsync <string>(); string expected = @" <Name>abc</Name> <Age>20</Age> <Address>武汉</Address> <Tel>12345678</Tel> <Email>[email protected]</Email> </Customer>".Trim(); Assert.IsTrue(actual.EndsWith(expected)); }
public async Task Test2() { HttpOption option = HttpOption.FromRawText(@" GET http://www.fish-mvc-demo.com/file-download/demo1/%E4%B8%AD%E6%96%87%E6%B1%89%E5%AD%97%E6%97%A0%E4%B9%B1%E7%A0%81~!%40%23%24%25%5E%26*()_%2B-%3D%3C%3E%3F%7C.txt HTTP/1.1 Host: www.fish-mvc-demo.com Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36 OPR/31.0.1889.174 Referer: http://www.fish-mvc-demo.com/Pages/Demo/TestFileDownload.aspx Accept-Encoding: gzip, deflate, lzma, sdch Accept-Language: zh-CN,zh;q=0.8 "); string actual = await option.SendAsync <string>(); string expected = Guid.Empty.ToString(); Assert.AreEqual(expected, actual); }