当前位置: 首页>>代码示例>>C#>>正文


C# HttpServer.GET方法代码示例

本文整理汇总了C#中HttpServer.GET方法的典型用法代码示例。如果您正苦于以下问题:C# HttpServer.GET方法的具体用法?C# HttpServer.GET怎么用?C# HttpServer.GET使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpServer的用法示例。


在下文中一共展示了HttpServer.GET方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ExampleCode

        public void ExampleCode()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                // simple basic usage, all subscriptions will run in a single event-loop
                server.GET("/hello/{Name}")
                      .Subscribe(ctx => ctx.Respond("Hello, " + ctx.Request.UriArguments.Name + "!"));

                Browser.ExecuteGet("http://localhost:1234/hello/George")
                       .ReadAllContent()
                       .Should().Be.EqualTo("Hello, George!");

                // use Rx LINQ operators
                server.POST("/hi/{Name}")
                      .Where(ctx => ctx.Request.UriArguments.Name == "George")
                      .Subscribe(ctx => ctx.Respond("Hi, George!"));

                server.POST("/hi/{Name}")
                      .Where(ctx => ctx.Request.UriArguments.Name == "Pete")
                      .Subscribe(ctx => ctx.Respond("Hi, Pete!"));

                Browser.ExecutePost("http://localhost:1234/hi/George")
                       .ReadAllContent()
                       .Should().Be.EqualTo("Hi, George!");

                Browser.ExecutePost("http://localhost:1234/hi/Pete")
                       .ReadAllContent()
                       .Should().Be.EqualTo("Hi, Pete!");

                // This becomes a problem:
                //Browser.ExecutePost("http://localhost:1234/hi/Fran").StatusCode.Should().Be.EqualTo(404);
            }
        }
开发者ID:NickLydon,项目名称:Anna,代码行数:33,代码来源:ServerTests.cs

示例2: CanHandleQueryStringArguments

        public void CanHandleQueryStringArguments()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                server.GET("customers")
                    .Subscribe(ctx => ctx.Respond(string.Format("customers where name equals to {0}", ctx.Request.QueryString.Name)));

                Browser.ExecuteGet("http://localhost:1234/customers?name=jose")
                    .ReadAllContent().Should().Be.EqualTo("customers where name equals to jose");
            }
        }
开发者ID:Amsoft-Systems,项目名称:Anna,代码行数:11,代码来源:ServerTests.cs

示例3: CanHandleUriArguments

        public void CanHandleUriArguments()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                server.GET("customer/{name}")
                      .Subscribe(ctx => ctx.Respond(string.Format("hello {0}", ctx.Request.UriArguments.name)));

                Browser.ExecuteGet("http://localhost:1234/customer/peter")
                    .ReadAllContent()
                    .Should().Be.EqualTo("hello peter");
            }
        }
开发者ID:Amsoft-Systems,项目名称:Anna,代码行数:12,代码来源:ServerTests.cs

示例4: CanReturnAStringFromWildcardURL

        public void CanReturnAStringFromWildcardURL()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                server.GET("/hello/{Name}")
                      .Subscribe(ctx => ctx.Respond("hello, " + ctx.Request.UriArguments.Name + "!"));

                Browser.ExecuteGet("http://localhost:1234/hello/George")
                       .ReadAllContent()
                       .Should().Be.EqualTo("hello, George!");
            }
        }
开发者ID:NickLydon,项目名称:Anna,代码行数:12,代码来源:ServerTests.cs

示例5: CanReturnAnString

        public void CanReturnAnString()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                server.GET("/")
                      .Subscribe(ctx => ctx.Respond("hello world"));

                Browser.ExecuteGet("http://localhost:1234")
                       .ReadAllContent()
                       .Should().Be.EqualTo("hello world");
            }
        }
开发者ID:NickLydon,项目名称:Anna,代码行数:12,代码来源:ServerTests.cs

示例6: CanReturnArbitraryHeader

        public void CanReturnArbitraryHeader()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                var headers = new Dictionary<string, string> { { "X-Something", "value" }, { "X-Another", "value again" } };
                server.GET("/test")
                      .Subscribe(ctx => ctx.Respond("I have some headers for you", 200, headers));

                HttpWebResponse response = Browser.ExecuteGet("http://localhost:1234/test");
                Dictionary<string, string> contentHeaders = response.Headers.ToDictionary().Select(pair => new KeyValuePair<string, string>(pair.Key, pair.Value.FirstOrDefault())).ToDictionary(pair => pair.Key, pair => pair.Value);
                foreach (var header in headers)
                    contentHeaders.Should().Contain(header);
            }
        }
开发者ID:NickLydon,项目名称:Anna,代码行数:14,代码来源:ServerTests.cs

示例7: WhenRequestingAnUnhandledRoute_ThenReturn404

        public void WhenRequestingAnUnhandledRoute_ThenReturn404()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                server.GET("customer/{name}")
                      .Subscribe(ctx => ctx.Respond(string.Format("hello {0}", ctx.Request.UriArguments.name)));

                Executing.This(() => Browser.ExecuteGet("http://localhost:1234/customersssss/peter"))
                    .Should().Throw<WebException>()
                    .And.Exception.Response.OfType<HttpWebResponse>().StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound);
            }
        }
开发者ID:Amsoft-Systems,项目名称:Anna,代码行数:12,代码来源:ServerTests.cs

示例8: CanReturnBinaryData

        public void CanReturnBinaryData()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                var expectedResponse = new byte[] { 0, 1, 2, 3, 4};
                server.GET("/")
                      .Subscribe(ctx => ctx.Response(expectedResponse).Send());

                var response = Browser.ExecuteGet("http://localhost:1234");
                byte[] data = new BinaryReader(response.GetResponseStream()).ReadBytes(1337);

                data.Length.Should().Be.EqualTo(expectedResponse.Length);
                for (int i = 0; i != data.Length; i++) data[i].Should().Be.EqualTo(expectedResponse[i]);
            }
        }
开发者ID:Amsoft-Systems,项目名称:Anna,代码行数:15,代码来源:ServerTests.cs

示例9: CanReturnAStaticFile

        public void CanReturnAStaticFile()
        {
            using (var server = new HttpServer("http://*:1234/"))
            {
                server.GET("/")
                      .Subscribe(ctx => ctx.StaticFileResponse(@"samples\example_1.txt").Send());

                Browser.ExecuteGet("http://localhost:1234")
                    .ReadAllContent()
                    .Should().Contain(string.Join(Environment.NewLine, Enumerable.Range(1, 9)));
            }
        }
开发者ID:Amsoft-Systems,项目名称:Anna,代码行数:12,代码来源:ServerTests.cs


注:本文中的HttpServer.GET方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。