Ejemplo n.º 1
0
        /// <summary>
        /// 用新的URL登录
        /// </summary>
        /// <param name="currentUrl"></param>
        /// <returns></returns>
        private Cookie Login(string currentUrl)
        {
            HttpOption option = HttpOption.FromRawText(_loginRequestRaw);

            Match m = _regex.Match(option.Url);

            if (m.Success == false)
            {
                throw new ArgumentException("登录请求中的URL格式不正确。");
            }

            Match m2 = _regex.Match(currentUrl);

            if (m2.Success == false)
            {
                throw new ArgumentException("当前待发送请求中的URL格式不正确。");
            }

            option.Url    = m2.Groups["host"].Value + m.Groups["path"].Value;
            option.Cookie = new CookieContainer();

            option.Send();

            var    cookies = option.Cookie.GetCookies(new Uri(m2.Groups["host"].Value));
            Cookie cookie  = cookies[_loginCookieName];

            if (cookie == null)
            {
                throw new InvalidOperationException("服务端没有返回指定名称的登录Cookie");
            }

            return(cookie);
        }
Ejemplo n.º 2
0
        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   = option.Send <string>();
            string expected = "A6DCC78B685D0CEA701CA90A948B9295F3685FDF";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
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   = option.Send <string>();
            string expected = "44D2D9635ED5CDEA2A858CD7A1CC2B0C";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        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   = option.Send <string>();
            string expected = "RmlzaCBMaQ==";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        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();
            }
        }
Ejemplo n.º 6
0
        public List <ResendResult> BatchSendRequest(string currentRequestRaw, int count)
        {
            HttpOption option = GetSendOption(currentRequestRaw);

            option.Timeout = 3000;

            bool stop = false;
            List <ResendResult> list = new List <ResendResult>();

            for (int i = 0; i < count; i++)
            {
                Stopwatch watch   = Stopwatch.StartNew();
                string    message = null;

                try {
                    option.Send();
                }
                catch (Exception ex) {
                    message = ex.Message;

                    // 如果有异常,就不需要再发请求了
                    stop = true;
                }

                watch.Stop();
                ResendResult result = new ResendResult {
                    ExecuteTime = watch.Elapsed, Message = message ?? "OK"
                };
                list.Add(result);

                if (stop)
                {
                    break;
                }
            }

            return(list);
        }
Ejemplo n.º 7
0
        public void Test1()
        {
            HttpOption option = HttpOption.FromRawText(@"
GET http://www.fish-web-demo.com/api/ns/TestSerializer/GetXml.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: null; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 1
");

            string xmlResult = option.Send <string>();
            string expected  = "</TestSerializerModel>";

            Assert.IsTrue(xmlResult.EndsWith(expected));



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test1_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/xml; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 2

" + xmlResult);

            string actual = option.Send <string>();

            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test2_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/xml; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Content-Length: 387
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 3

" + xmlResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
GET http://www.fish-web-demo.com/api/ns/TestSerializer/GetJson.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: null; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Cookie: PageStyle=Style2
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 4
");

            string jsonResult = option.Send <string>();

            Assert.IsTrue(jsonResult.StartsWith("{") && jsonResult.EndsWith("}"));



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test1_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 5

" + jsonResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test2_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 6

" + jsonResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
GET http://www.fish-web-demo.com/api/ns/TestSerializer/GetXml2.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: null; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 7

");

            xmlResult = option.Send <string>();
            expected  = "</TestSerializerModel2>";
            Assert.IsTrue(xmlResult.EndsWith(expected));



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test3_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/xml; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 8

" + xmlResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test4_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/xml; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 9

" + xmlResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
GET http://www.fish-web-demo.com/api/ns/TestSerializer/GetJson2.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: null; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 10
");

            jsonResult = option.Send <string>();
            Assert.IsTrue(jsonResult.StartsWith("{") && jsonResult.EndsWith("}"));



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test3_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 11

" + jsonResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test4_AutoCheck.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 12

" + jsonResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
GET http://www.fish-web-demo.com/api/ns/TestSerializer/GetXml5.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: null; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 13

");

            xmlResult = option.Send <string>();
            expected  = "</TestSerializerModel3>";
            Assert.IsTrue(xmlResult.EndsWith(expected));



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test5.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/xml; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 14

" + xmlResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test6.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/xml; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 15

" + xmlResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
GET http://www.fish-web-demo.com/api/ns/TestSerializer/GetJson5.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: null; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 16
");

            jsonResult = option.Send <string>();
            Assert.IsTrue(jsonResult.StartsWith("{") && jsonResult.EndsWith("}"));



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test5.aspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 17

" + jsonResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);



            option = HttpOption.FromRawText(@"
POST http://www.fish-web-demo.com/api/ns/TestSerializer/Test6.caspx HTTP/1.1
Host: www.fish-ClownFish.Web-demo.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/plain, */*; q=0.01
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://www.fish-web-demo.com/Pages/Demo/TestSerializer.htm
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
test: 18

" + jsonResult);

            actual   = option.Send <string>();
            expected = "1";
            Assert.AreEqual(expected, actual);
        }